【问题标题】:How do i traverse this json data in java? [duplicate]我如何在java中遍历这个json数据? [复制]
【发布时间】:2017-08-12 09:42:17
【问题描述】:

您好,我想使用 android 快速网络库遍历此 json 数据,但我无法这样做。有人可以建议一种方法来获取数组中所有对象的 id 和文件名吗?

注意 - 必须使用 ANDROID 快速网络库 -https://github.com/amitshekhariitbhu/Fast-Android-Networking

 [
          {
            "_id": "598ae8773376673353ef6da6",
            "filename": "nldcjjzpesyhkkx1502275703450.jpg",
            "votes": 33,
            "__v": 0
          },
          {
            "_id": "598ae83b3376673353ef6da5",
            "filename": "xlwkfcxicwhqibw1502275643486.jpg",
            "votes": 31,
            "__v": 0
          },
          {
            "_id": "598a2e7000c6717c3c04e534",
            "filename": "ndkiqptbifqmnjz1502228078447.jpg",
            "votes": 19,
            "__v": 0
          },
          {
            "_id": "5989ece9673738c027377710",
            "filename": "seicmgkzubzavfy1502211303478.jpg",
            "votes": 13,
            "__v": 0
          }
]

【问题讨论】:

    标签: java android json


    【解决方案1】:

    您可以使用GSON lib,也可以使用 for 循环检索,如下所示:

    try {
        JSONArray jsonArray = new JSONArray(yourResponseStrOrJson);
        for(int i = 0; i < jsonArray.length(); i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Log.d("ID", jsonObject.getString("_id"));
            //TOTO retrive other element
        }
    }catch (JSONException e){
      e.printStackTrace();
    }
    

    【讨论】:

    • 不,我需要使用 android 快速网络库 - 这是链接 github.com/amitshekhariitbhu/Fast-Android-Networking
    • @Knightcube,把上面的代码放在 onResponse 方法中,你尝尝吗
    • 是的,它现在正在工作。实际上,我还有其他一些连接问题,这就是它无法正常工作的原因。我的坏
    • 很高兴工作代码成功
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 2012-04-10
    • 1970-01-01
    相关资源
    最近更新 更多