JSON数组无法获取数据

时间:2016-06-23 12:45:56

标签: java android json

所以从JSON看起来像这样:

"tracks":{  
      "track":[  
         {  
            "name":"Do I Wanna Know?",
            "duration":"272",
            "listeners":"711568",
            "mbid":"f1e57531-e0df-4b3e-938f-1ae30c5b1a11",
            "url":"http://www.last.fm/music/Arctic+Monkeys/_/Do+I+Wanna+Know%3F",
            "streamable":{  
               "#text":"0",
               "fulltrack":"0"
            },

我正在尝试获取track数组中的第一个“name”字段。

我正在尝试使用此代码:

JSONArray pjesma = jsonObject.getJSONArray("track");
                        JSONObject imePjesme = pjesma.getJSONObject(0);
                        String pjesmaTekst = imePjesme.getString("name");

但它只是告诉我有No value for track

我做错了什么?

完整代码:

   private void getData(String country){
        informacije.clear();
        String finalUrl = urlLeft+country+urlRight;

        JsonObjectRequest mainRequest = new JsonObjectRequest(Request.Method.GET, finalUrl, (JSONObject) null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONObject pjesmaObject = response.getJSONObject("tracks");
                    JSONArray pjesmaArray = pjesmaObject.getJSONArray("track");

                    for (int i = 0; i < pjesmaArray.length(); i++){
                        JSONObject jsonObject = pjesmaArray.getJSONObject(i);

                        // ARTIST
                        JSONObject imeIzvodjaca = jsonObject.getJSONObject("artist");
                        String izvodjacTekst = imeIzvodjaca.getString("name");

                        // PJESMA
                        JSONArray pjesma = jsonObject.getJSONArray("track");
                        JSONObject imePjesme = pjesma.getJSONObject(0);
                        String pjesmaTekst = imePjesme.getString("name");

                        //SLIKA
                        JSONArray imageArray = jsonObject.getJSONArray("image");
                        JSONObject largeImage = imageArray.getJSONObject(3);
                        String image = largeImage.getString("#text");

                        Informacije info = new Informacije();
                        info.setArtist(izvodjacTekst);
                        info.setTitle(pjesmaTekst);
                        info.setUrl(image);

                        informacije.add(info);
                    }

                    adapter.notifyDataSetChanged();

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

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        AppController.getInstance().addToRequestQueue(mainRequest);
    }

JSON响应

06-23 14:43:38.167 16255-16255/com.darioradecic.topmusicandartists W/System.err: org.json.JSONException: No value for track
06-23 14:43:38.170 16255-16255/com.darioradecic.topmusicandartists W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
06-23 14:43:38.170 16255-16255/com.darioradecic.topmusicandartists W/System.err:     at org.json.JSONObject.getJSONArray(JSONObject.java:584)
06-23 14:43:38.170 16255-16255/com.darioradecic.topmusicandartists W/System.err:     at com.darioradecic.topmusicandartists.MainActivity$1.onResponse(MainActivity.java:63)
06-23 14:43:38.170 16255-16255/com.darioradecic.topmusicandartists W/System.err:     at com.darioradecic.topmusicandartists.MainActivity$1.onResponse(MainActivity.java:48)
06-23 14:43:38.170 16255-16255/com.darioradecic.topmusicandartists W/System.err:     at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:72)
06-23 14:43:38.170 16255-16255/com.darioradecic.topmusicandartists W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
06-23 14:43:38.170 16255-16255/com.darioradecic.topmusicandartists W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
06-23 14:43:38.170 16255-16255/com.darioradecic.topmusicandartists W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
06-23 14:43:38.170 16255-16255/com.darioradecic.topmusicandartists W/System.err:     at android.os.Looper.loop(Looper.java:211)
06-23 14:43:38.170 16255-16255/com.darioradecic.topmusicandartists W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5389)
06-23 14:43:38.170 16255-16255/com.darioradecic.topmusicandartists W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
06-23 14:43:38.170 16255-16255/com.darioradecic.topmusicandartists W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
06-23 14:43:38.170 16255-16255/com.darioradecic.topmusicandartists W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
06-23 14:43:38.170 16255-16255/com.darioradecic.topmusicandartists W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

3 个答案:

答案 0 :(得分:0)

试试这个:

JSONObject pjesma = jsonObject.getJSONObject ("tracks");
JSONArray imePjesme = pjesma.getJSONArray ("track");
String pjesmaTekst = imePjesme.getJSONObject (0).getString("name");

如果它不起作用,请发布jsonObject var

的内容

答案 1 :(得分:0)

试试这个:

JSONObject tracksObject = jsonObject.getJSONObject("tracks");
JSONArray pjesma = tracksObject.getJSONArray("track");
JSONObject imePjesme = pjesma.getJSONObject(0);
String pjesmaTekst = imePjesme.getString("name");

答案 2 :(得分:-1)

解决方案:

对不起,我真是个傻瓜!洗脱是如此简单。

而不是:

JSONArray pjesma = jsonObject.getJSONArray("track");
                        JSONObject imePjesme = pjesma.getJSONObject(0);
                        String pjesmaTekst = imePjesme.getString("name");

所需要的只是:

String pjesmaTekst = jsonObject.getString("name");