1

我如何查看这个 Json 结构并存储每个元素 id 名称链接 picutre nb_album nb_fan 收音机,输入列表。这是我的 json 文件。

 {
        "data": [
            {
                "id": "1214294",
                "name": "The Pop Rock Boys",
                "link": "http://www.deezer.com/artist/1214294",
                "picture": "https://api.deezer.com/2.0/artist/1214294/image",
                "nb_album": 7,
                "nb_fan": 3,
                "radio": false,
                "type": "artist"
            },
            {
                "id": "813196",
                "name": "Ringtone Pop Rock",
                "link": "http://www.deezer.com/artist/813196",
                "picture": "https://api.deezer.com/2.0/artist/813196/image",
                "nb_album": 0,
                "nb_fan": 4,
                "radio": false,
                "type": "artist"
            },
            {
                "id": "4165034",
                "name": "Rock of Pop",
                "link": "http://www.deezer.com/artist/4165034",
                "picture": "https://api.deezer.com/2.0/artist/4165034/image",
                "nb_album": 1,
                "nb_fan": 0,
                "radio": false,
                "type": "artist"
            },
            {
                "id": "4022223",
                "name": "instrumental / Pop / Rock",
                "link": "http://www.deezer.com/artist/4022223",
                "picture": "https://api.deezer.com/2.0/artist/4022223/image",
                "nb_album": 0,
                "nb_fan": 1,
                "radio": false,
                "type": "artist"
            }
        ],
        "total": 4
    }
4

1 回答 1

6

您正在将初始字符串解析为数组。它不是一个数组,它是一个包含一个名为“data”的数组的对象。您需要将其解析为 JSONObject,然后从中获取名为 data 的数组,然后像您一样循环。

    try {
         JSONArray articlesArray = new JSONObject(jString).getJSONArray("data");
         JSONObject aObject;
         for(int i=0; i<articlesArray.length(); i++){
             aObject=articlesArray.getJSONObject(i);

             SearchTrack a=new SearchTrack();
             a.setId(aObject.getString("id"));
             a.setLink(aObject.getString("link"));
             a.setName(aObject.getString("name"));
             a.setPicture(aObject.getString("picture"));
             a.setNbAlbum(aObject.getString("nb_album"));
             a.setNbFan(aObject.getString("nb_fan"));
             a.setRadio(aObject.getString("radio"));
             a.setType(aObject.getString("type"));

             articles.add(a);
         }
     }catch (JSONException e) {
         e.printStackTrace();
     }
于 2013-06-27T06:32:46.170 回答