我如何解析这个Json数组?

时间:2016-03-21 16:41:05

标签: android json parsing

我必须解析下面的Json。我知道如何在JsonArray有钥匙的情况下从JsonArray获取Json。如果我没有钥匙,我该怎么做?

[
  {
    "kind": "track",
    "id": 253792869,
    "created_at": "2016/03/21 15:20:47 +0000",
    "user_id": 167064157,
    "duration": 9457,
    "commentable": true,
    "state": "finished",
    "original_content_size": 375488,
    "last_modified": "2016/03/21 15:20:48 +0000",
    "sharing": "public",
    "tag_list": "commercial",
    "permalink": "2016-03-08_lbs_bigfm_spot_3",
    "streamable": true,
    "embeddable_by": "all",
    "downloadable": false,
    "purchase_url": null,
    "label_id": null,
    "purchase_title": null,
    "genre": "",
    "title": "2016-03-08_LBS_BigFM_SPOT_3",
    "description": "",
    "label_name": null,
    "release": null,
    "track_type": null,
    "key_signature": null,
    "isrc": null,
    "video_url": null,
    "bpm": null,
    "release_year": null,
    "release_month": null,
    "release_day": null,
    "original_format": "mp3",
    "license": "all-rights-reserved",
    "uri": "https://api.soundcloud.com/tracks/253792869",
    "user": {
      "id": 167064157,
      "kind": "user",
      "permalink": "user690075536",
      "username": "user690075536",
      "last_modified": "2016/02/26 08:19:23 +0000",
      "uri": "https://api.soundcloud.com/users/167064157",
      "permalink_url": "http://soundcloud.com/user690075536",
      "avatar_url": "https://i1.sndcdn.com/avatars-000159639971-j1awdb-large.jpg"
    },
    "permalink_url": "http://soundcloud.com/user690075536/2016-03-08_lbs_bigfm_spot_3",
    "artwork_url": "https://i1.sndcdn.com/artworks-000152760877-s9mcat-large.jpg",
    "waveform_url": "https://w1.sndcdn.com/8Ijd5YlUn9iW_m.png",
    "stream_url": "https://api.soundcloud.com/tracks/253792869/stream",
    "playback_count": 0,
    "download_count": 0,
    "favoritings_count": 0,
    "comment_count": 0,
    "attachments_uri": "https://api.soundcloud.com/tracks/253792869/attachments"
  }
]

4 个答案:

答案 0 :(得分:0)

有效的JSON:

{
   "abc":[
      {
         "name":"Ram"
      },
      {
         "name":"Shyam"
      },
      {
         "name":"Mohan"
      },
      {
         "name":"Pankaj"
      },
      {
         "name":"Komal"
      }
   ]
}

答案 1 :(得分:0)

try 
{
    JSONArray jsonArray = new JSONArray(jsonStrFromSoundCloud);

    for (int i = 0; i < jsonArray.length(); i++) {
        //loop in array
    }

} catch (JSONException e) {
        e.printStackTrace();
}

答案 2 :(得分:0)

我得到了答案,我将这个数组绑定在一个json对象中。然后获取它。 像这样。

"{\"data\":"+ jsonArray+"}";

答案 3 :(得分:0)

我试图通过创建一个json数组来解决类似的问题。

[{“name”:“dave”,     “时代”:23     },     {     “名字”:“德克斯特”     “年龄”:24     }]

这是我的POJO

public class MyPojo {

private String name;
private int age;
// Getter & Setters
}

这就是你需要做的就是将JSON解析为List对象。

Type listType = new TypeToken<ArrayList<MyPojo>>(){}.getType();

List<MyPojo> jsonObject = new Gson().fromJson(response, listType);

希望这会有所帮助。 :)

修改

不要忘记导入:import java.lang.reflect.Type;