【问题标题】:how to parse JSON response from php associated array in android?如何从android中的php关联数组解析JSON响应?
【发布时间】:2019-03-01 04:17:57
【问题描述】:

我有一个对 php 服务器的 android 请求,它以 JSON 格式发送一个关联的数组,如何通过改造或 volley 将其解析为模型或其他方式? 像这样的 JSON 响应:

{
   "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
            }
         }
      }
   }
}

【问题讨论】:

标签: php android json web-services associative-array


【解决方案1】:

如果你使用 volley,你可以使用 JSONRequest 获取一个 json 对象(或者如果你使用 Retrofit,你可以将 String 转换为 json 对象),然后使用我的代码获取数组。 (如果您有任何问题,请发表评论或联系我: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

}

【讨论】:

  • 非常感谢......它工作......但我认为如果json响应比这复杂,它很难建立模型......
  • 我不这么认为。如果您在构建模型时遇到任何问题,请随时与我联系。
猜你喜欢
  • 2013-08-20
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多