2016-03-21 98 views
0

我必須解析下面的Json。我知道如何從JsonArray中獲得Json時,它有一個關鍵。我沒有任何鑰匙時如何做到這一點?我該如何解析這個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" 
    } 
] 
+0

此「JSON」無效... –

+0

https://api.soundcloud.com/tracks?client_id=016f9f3049b3fb596310b26e65aef484碰到此API。迴應類似於上述問題。 –

+0

來自API的JSON [有效](https://jsonformatter.curiousconcept.com/)。 –

回答

0

有效的JSON:

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

我知道wat是有效的json –

+0

我更新我的問題。現在檢查 –

0
try 
{ 
    JSONArray jsonArray = new JSONArray(jsonStrFromSoundCloud); 

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

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

不工作..我已經嘗試過 –

0

我有我的回答,我結合這個數組JSON對象內。然後取出它。 類似這樣。

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

我想這是一個解決方法,因爲每個人可能沒有奢侈品來改變JSON。 –

0

我試圖通過創建一個json數組來解決類似的問題。

[{ 「Name」: 「戴夫」, 「時代」: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); 

希望這會有所幫助。 :)

編輯

不要忘記導入:java.lang.reflect.Type;