【问题标题】:Decode nested JSON array in Android在 Android 中解码嵌套的 JSON 数组
【发布时间】:2017-09-30 19:07:21
【问题描述】:

我有一个要解码的嵌套 JSON 数组。我已经找到了一种方法来解码对象中的指定信息,但效果不是很好。

这是我用来从 JSON 对象中解码(名称、封面和街道)的代码:

JSONArray jsonArray = response.getJSONArray("data");

for (int i=0; i < jsonArray.length();i++){
    JSONObject events = jsonArray.getJSONObject(i);
    String name = events.getString("name");
    JSONObject cover = events.getJSONObject("cover");
    String imgurl = cover.getString("source");
    JSONObject place = events.getJSONObject("place");
    JSONObject loc = place.getJSONObject("location");
    String street = loc.getString("street");

    ItemListView item = new ItemListView(name,street,imgurl);
    listItems.add(item);
    adapter = new ItemListAdapter(listItems, getActivity());

    recyclerView.setAdapter(adapter);
}

我想解码更多信息,例如描述、结束时间和开始时间。

JSON 数据结构如下所示:

{
   "data": [
      {
         "description": "",
         "end_time": "",
         "name": "",
         "place": {
            "name": "",
            "location": {
               "city": "",
               "country": "",
               "latitude": 0000000,
               "longitude": 000000,
               "street": ""
            },
            "id": ""
         },
         "start_time": "",
         "id": ""
      },
      {
         "description": "",
         "end_time": "",
         "name": "",
         "place": {
            "name": "",
            "location": {
               "city": "",
               "country": "",
               "latitude": 0000000,
               "longitude": 000000,
               "street": ""
            },
            "id": ""
         },
         "start_time": "",
         "id": ""
      }
    ]
}

我将解码后的信息放在我的 Android 应用程序的 ListView 中,这就是为什么我在代码中有一个适配器和 ListView。

非常感谢任何帮助:) 提前致谢。

【问题讨论】:

    标签: android arrays json nested decode


    【解决方案1】:

    这很简单,你可以得到其他这样的字符串 -:

    JSONArray jsonArray = response.getJSONArray("data");
    
                for (int i=0; i < jsonArray.length();i++){
                    JSONObject events = jsonArray.getJSONObject(i);
                    String description= events.getString("description");
                    String end_time= events.getString("end_time");
                    String start_time= events.getString("start_time");
    
                }
    

    【讨论】:

    • 天啊,我自己弄的这么复杂,非常感谢:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2018-05-01
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    相关资源
    最近更新 更多