Youtube ID JSON Integration



您好,有人可以解释一下以下视频ID代码的Youtube集成android Json的视频ID代码。

String videoID = (String) ((JSONObject) ((JSONObject) jsonObject.getJSONArray("items").get(0)).get("id")).get("videoId");

这是一个使用 java 对象强制转换的单行。而不是使用特定的函数getJSONObject/getJSONArray/getString,是不是只使用get+类型转换。

一个更容易(和更长(的版本是:

// Get the items array from the json object
JSONArray  items = jsonObject.getJSONArray("items")  // equivalent of (JSONArray) jsonObject.get("item")
// Get the first item at index 0
JSONObject item0 = items.getJSONObject(0)  // equivalent of (JSONObject) items.get(0)
// Get the object "id" from the first item
JSONObject idObj = item0.getJSONObject("id") // equivalent of (JSONObject) item0.get("id")
// Get the string video from the object "id"
String videoId   = idObj.getString("videoId") // equivalent of (String) idObj.get("videoId")

或一行:

String videoId = jsonObject.getJSONArray("items")
                           .getJSONObject(0)
                           .getJSONObject("id")
                           .getString("videoId")

最新更新