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
            }
         }
      }
   }
}
Ghoti
  • 2,388
  • 1
  • 18
  • 22
hkh114
  • 162
  • 1
  • 3
  • 15

1 Answers1

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: nhat.thtb@gmail.com)

     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

}
NhatVM
  • 1,964
  • 17
  • 25
  • Thanks alot ... it work ... but I think if the json response is complicated than this , its hard to build models ... – hkh114 Sep 26 '18 at 07:22
  • I don't think so. If you have any issues when build your models, feel free contact me. – NhatVM Sep 26 '18 at 08:30