Android JSON嵌套解析器

时间:2015-03-22 03:01:51

标签: java android json

我想解析艺术家的身份证明" Justin Timberlake"从这个嵌套的JSON,但它疯狂,我在这里尝试了很多方法和许多例子,但它仍然没有在我的测试应用程序上显示id。顺便说一句,我不是很有经验。

这是JSON:

{
"resultsPage": {
"status": "ok",
"results": {
  "artist": [
    {
      "onTourUntil": null,
      "identifier": [
        {...}
      ],
      "uri": "http://www.songkick.com/artists/209003-justin-timberlake?utm_source=31833&utm_medium=partner",
      "id": 209003,
      "displayName": "Justin Timberlake"
    },
    {
      "identifier": [

      ],
      "uri": "http://www.songkick.com/artists/7570864-justin-timberlake-at-united-center?utm_source=31833&utm_medium=partner",
      "displayName": "Justin Timberlake at United Center",
      "id": 7570864,
      "onTourUntil": null
    },
    {
      "onTourUntil": null,
      "displayName": "Broadway Sings Justin Timberlake",
      "identifier": [

      ],
      "uri": "http://www.songkick.com/artists/8106393-broadway-sings-justin-timberlake?utm_source=31833&utm_medium=partner",
      "id": 8106393
    },
    {
      "onTourUntil": null,
      "identifier": [

      ],
      "uri": "http://www.songkick.com/artists/7859354-justin-timberlake-and-jay-z-tribut?utm_source=31833&utm_medium=partner",
      "displayName": "Justin Timberlake & Jay Z Tribut",
      "id": 7859354
    }
  ]
},
"perPage": 50,
"page": 1,
"totalEntries": 4
}
}

这是我的Java:

 private void parseJSONResponse(JSONObject response){
    if (response == null || response.length()==0){
        return;
    }
    try {

        StringBuilder data = new StringBuilder();
        JSONArray arrayIds = response.getJSONObject("resultsPage").getJSONObject("results").getJSONArray("artists");
        for (int i=0; i<arrayIds.length(); i++){

            String currentID= arrayIds.getJSONArray(i).getString(3); //trying to get ID located at position 4.

            data.append(currentID+"\n");
        }

        TextView Id = (TextView) findViewById( R.id.artist );
        Id.setText(data.toString());



    }catch (JSONException e){

    }
}

由于这个JSON显示了关于艺术家的其他无关信息,而且我只想要displayName:&#34; Justin Timberlake&#34;的ID,我是否需要输入这样的if语句:

for (int i=0; i<arrayIds.length(); i++) {
            if (arrayIds.getJSONObject(i).equals("Justin Timberlake")) {
                String currentID = arrayIds.getJSONArray(i).getString(3);
//trying to get ID located at position 4.
                TextView Id = (TextView) findViewById(R.id.artist);
                Id.setText(data.toString());
            }
        }

1 个答案:

答案 0 :(得分:0)

这有效:

<强>被修改

   String json = "{\n" +
                "\"resultsPage\": {\n" +
                "\"status\": \"ok\",\n" +
                "\"results\": {\n" +
                "  \"artist\": [\n" +
                "    {\n" +
                "      \"onTourUntil\": null,\n" +
                "      \"identifier\": [\n" +
               // "        {...}\n" +
                "      ],\n" +
                "      \"uri\": \"http://www.songkick.com/artists/209003-justin-timberlake?utm_source=31833&utm_medium=partner\",\n" +
                "      \"id\": 209003,\n" +
                "      \"displayName\": \"Justin Timberlake\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"identifier\": [\n" +
                "\n" +
                "      ],\n" +
                "      \"uri\": \"http://www.songkick.com/artists/7570864-justin-timberlake-at-united-center?utm_source=31833&utm_medium=partner\",\n" +
                "      \"displayName\": \"Justin Timberlake at United Center\",\n" +
                "      \"id\": 7570864,\n" +
                "      \"onTourUntil\": null\n" +
                "    },\n" +
                "    {\n" +
                "      \"onTourUntil\": null,\n" +
                "      \"displayName\": \"Broadway Sings Justin Timberlake\",\n" +
                "      \"identifier\": [\n" +
                "\n" +
                "      ],\n" +
                "      \"uri\": \"http://www.songkick.com/artists/8106393-broadway-sings-justin-timberlake?utm_source=31833&utm_medium=partner\",\n" +
                "      \"id\": 8106393\n" +
                "    },\n" +
                "    {\n" +
                "      \"onTourUntil\": null,\n" +
                "      \"identifier\": [\n" +
                "\n" +
                "      ],\n" +
                "      \"uri\": \"http://www.songkick.com/artists/7859354-justin-timberlake-and-jay-z-tribut?utm_source=31833&utm_medium=partner\",\n" +
                "      \"displayName\": \"Justin Timberlake & Jay Z Tribut\",\n" +
                "      \"id\": 7859354\n" +
                "    }\n" +
                "  ]\n" +
                "},\n" +
                "\"perPage\": 50,\n" +
                "\"page\": 1,\n" +
                "\"totalEntries\": 4\n" +
                "}\n" +
                "}";


try {

    JSONObject jsonRootObject = new JSONObject(json);

    JSONObject resultsPager = jsonRootObject.getJSONObject("resultsPage");

    JSONObject results = resultsPager.getJSONObject("results");

    JSONArray artist = results.getJSONArray("artist");

    for(int i=0; i < artist.length(); i++){

        JSONObject jsonObject = artist.getJSONObject(i);

         if(jsonObject.getString("displayName").equals("Justin Timberlake")){

                Log.d("json"," ->> Justin Timberlake");
         }
    }


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

您的代码:

private void parseJSONResponse(JSONObject response){

    if (response == null || response.length()==0){
        return;
    }

    try {

        StringBuilder data = new StringBuilder();


        JSONObject resultsPager = response.getJSONObject("resultsPage");

        JSONObject results = resultsPager.getJSONObject("results");

        JSONArray artist = results.getJSONArray("artist");

        for(int i=0; i < artist.length(); i++){

            JSONObject jsonObject = artist.getJSONObject(i);

            if(jsonObject.getString("displayName").equals("Justin Timberlake")){

                Log.d("json"," ->> Justin Timberlake");
            }
        }

    }catch (JSONException e){

    }
}