如何在Android中显示Json Parse值?

时间:2016-01-27 19:25:02

标签: android json

我正在尝试解析和查看json数据。数据未显示在我的设备上。通常我可以多次这样做,但在这种情况下我不能。

这是我的代码:

@Override
    protected Void doInBackground(Void... params) {
        // Create an array
        arraylist = new ArrayList<HashMap<String, String>>();

        // Retrieve JSON Objects from the given URL address


        jsonobject = JSONfunctions.getJSONfromURL("https://www.googleapis.com/youtube/v3/search?part=id%2Csnippet&order=viewCount&q=minecraft+mods&maxResults=50&key=<REDACTED>");

        try {
            // Locate the array name in JSON
            JSONArray jsonarray = jsonobject.getJSONArray("items");

            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);
                // Retrive JSON Objects
                JSONObject jsonObjId = jsonobject.getJSONObject("id");
                map.put("videoId", jsonObjId.getString("videoId"));

                JSONObject jsonObjSnippet = jsonobject.getJSONObject("snippet");
                map.put("title", jsonObjSnippet.getString("title"));

                //map.put("description", jsonObjSnippet.getString("description"));
                // map.put("flag", jsonobject.getString("flag"));

                JSONObject jsonObjThumbnail = jsonObjSnippet.getJSONObject("thumbnails");
                String imgURL = jsonObjThumbnail.getJSONObject("high").getString("url");
                map.put("url",imgURL);


                // Set the JSON Objects into the array
                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

这是我的Json Respons

{  
"items": [
  {
   "kind": "youtube#searchResult",
   "etag": "\"abQHWywil_AkNqdqji7_FqiK-u4/sYlQoR4h5RdFR24l2b2rLPG5lwA\"",
   "id": {
    "kind": "youtube#channel",
    "channelId": "UCH-_hzb2ILSCo9ftVSnrCIQ"
   },
   "snippet": {
    "publishedAt": "2008-07-09T21:56:56.000Z",
    "channelId": "UCH-_hzb2ILSCo9ftVSnrCIQ",
    "title": "YOGSCAST Lewis & Simon",
    "description": "Minecraft and multiplayer comedy gaming with a drunken dwarf and a handsome spaceman! Join us as we laugh our way through the best, the worst and the ...",
    "thumbnails": {
     "default": {
      "url": "https://yt3.ggpht.com/-FMO2nSO2pP8/AAAAAAAAAAI/AAAAAAAAAAA/QZLWwqsqMIU/s512-c-k-no/photo.jpg"
     },
     "medium": {
      "url": "https://yt3.ggpht.com/-FMO2nSO2pP8/AAAAAAAAAAI/AAAAAAAAAAA/QZLWwqsqMIU/s512-c-k-no/photo.jpg"
     },
     "high": {
      "url": "https://yt3.ggpht.com/-FMO2nSO2pP8/AAAAAAAAAAI/AAAAAAAAAAA/QZLWwqsqMIU/s512-c-k-no/photo.jpg"
     }
    },
    "channelTitle": "BlueXephos",
    "liveBroadcastContent": "none"
   }
  }]
}

1 个答案:

答案 0 :(得分:0)

如下图所示,"videoId"中没有JSONObject jsonObjId个关键字。

您的地图对象中null键的值为"videoId"。可能这就是为什么你的数据没有显示的原因。

PS:使用模型类而不是HashMap,它会更优雅。