0

I have an android request to php server that it send an associated array in JSON , how to parse it to model or other way with retrofit or volley ? JSON response like this:

{
   "d": {
      "240": {
         "title": "First floor",
         "rooms": {
            "246": {
               "title": "kitchen",
               "type": 1,
               "hid": 246
            },
            "251": {
               "title": "room56",
               "type": 3,
               "hid": 251
            }
         }
      },
      "389": {
         "title": "Second   floor",
         "rooms": {
            "390": {
               "title": "First room",
               "type": 2,
               "hid": 390
            }
         }
      }
   }
}
3
  • You just want to make it an array? json_decode($array, true); Commented Sep 25, 2018 at 13:54
  • stackoverflow.com/questions/6773474/… Commented Sep 25, 2018 at 13:55
  • Spelled parse correctly and formatted the JSON so that it's readable Commented Sep 25, 2018 at 14:57

1 Answer 1

1

If you use volley, you can get a json object with JSONRequest (or if you use Retrofit you can convert String to json object) and then use my code to get array. ( If you have any issues please comment or contact me: [email protected])

     protected ArrayList<Floor> parse(JSONObject json_response) {

    ArrayList<Floor> list_floor = new ArrayList<>();

    try {
        JSONObject json_d = json_response.getJSONObject("d");
        Iterator<String> iter = json_d.keys();
        while (iter.hasNext()) {
            String key = iter.next();
            JSONObject json_floor = json_d.getJSONObject(key);
            Floor floor = new Floor();
            floor.parse(json_floor);
            list_floor.add(floor);
        }
    } catch (Exception e) {

    }

    return list_floor;

}

 public class Entity {
    public void parse(JSONObject json) {

    }
}

public class Floor extends Entity {
    private String mTitle;
    private ArrayList<Room> mListRoom;

    @Override
    public void parse(JSONObject json) {
        try {
            mTitle = json.getString("title");
            mListRoom = new ArrayList<>();
            JSONObject js_room = json.getJSONObject("rooms");
            Iterator<String> iter = js_room.keys();
            while (iter.hasNext()) {
                String key = iter.next();
                JSONObject js_room_tmp = json.getJSONObject(key);
                Room room = new Room();
                room.parse(js_room_tmp);
                mListRoom.add(room);
            }
        } catch (Exception e) {

        }
    }

    // setter and getter
}

public class Room extends Entity {
    private String mTitle;
    private int mType;
    private int mHid;

    @Override
    public void parse(JSONObject json) {
        try {
            mTitle = json.getString("title");
            mType = json.getInt("type");
            mHid = json.getInt("hid");
        } catch (Exception e) {

        }
    }
    // setter and getter

}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks alot ... it work ... but I think if the json response is complicated than this , its hard to build models ...
I don't think so. If you have any issues when build your models, feel free contact me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.