无法从moviedb api解析id

时间:2015-09-11 12:05:20

标签: json parsing android-volley

您好我正在尝试从数组中解析出id。我有一个显示祝酒的日志记录方法:

StringBuilder data= new StringBuilder();

JSONArray arrayMovies= response.getJSONArray(KEY_MOVIES);

for (int i = 0; i < arrayMovies.length(); i++) {
    JSONObject currentMovie = arrayMovies.getJSONObject(i);

    String id = currentMovie.getString(KEY_ID);

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

L.t(getActivity(), data.toString());
.....

但它没有解析出来。刚刚获得电影列表。

这是json文件:

{
    "page": 1,
    "results": [
        {
            "adult": false,
            "backdrop_path": "/tbhdm8UJAb4ViCTsulYFL3lxMCd.jpg",
            "genre_ids": [
                53,
                28,
                12
            ],
            "id": 76341,
            "original_language": "en",
            "original_title": "Mad Max: Fury Road",
            "overview": "An apocalyptic story set in the furthest reaches of our planet, in a stark desert landscape where humanity is broken, and most everyone is crazed fighting for the necessities of life. Within this world exist two rebels on the run who just might be"
        }
    ]
}

2 个答案:

答案 0 :(得分:0)

不确定,但可能因为id值没有引号,因此无法使用getString()方法解析为String

答案 1 :(得分:0)

我认为您的代码没问题,只需要再次检查响应和密钥,我已经使用硬编码测试了响应为String和2键如下:

        String jsonString = "{\n" +
                "    \"page\": 1,\n" +
                "    \"results\": [\n" +
                "        {\n" +
                "            \"adult\": false,\n" +
                "            \"backdrop_path\": \"/tbhdm8UJAb4ViCTsulYFL3lxMCd.jpg\",\n" +
                "            \"genre_ids\": [\n" +
                "                53,\n" +
                "                28,\n" +
                "                12\n" +
                "            ],\n" +
                "            \"id\": 76341,\n" +
                "            \"original_language\": \"en\",\n" +
                "            \"original_title\": \"Mad Max: Fury Road\",\n" +
                "            \"overview\": \"An apocalyptic story set in the furthest reaches of our planet, in a stark desert landscape where humanity is broken, and most everyone is crazed fighting for the necessities of life. Within this world exist two rebels on the run who just might be\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        StringBuilder data= new StringBuilder();
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            JSONArray arrayMovies= jsonObject.getJSONArray("results");

            for (int i = 0; i < arrayMovies.length(); i++) {
                JSONObject currentMovie = arrayMovies.getJSONObject(i);
                String id = currentMovie.getString("id");
                data.append(id+"\n");
            }
            Log.i("BNK", data.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

这是截图(请关注logcat):

enter image description here