我有一個網址:https://gdata.youtube.com/feeds/api/users/charlieissocoollike/uploads?alt=jsonc&v=2,它提供有關用戶上傳的最新YouTube信息的JSON信息。從YouTube API解析JSON
我寫了一些代碼來解析這個JSON數據,但我不明白JSON是如何工作的以及如何在Java中解析它。
public void getVideoData() throws ClientProtocolException, JSONException, IOException {
JSONObject object = (JSONObject) new JSONTokener(getVideoJSON().toString()).nextValue();
//String query = object.getString("data");
JSONArray locations = object.getJSONArray("data");
output.setText(locations.getString(1));
}
public JSONObject getVideoJSON() throws ClientProtocolException, IOException, JSONException {
final String URL = "https://gdata.youtube.com/feeds/api/users/charlieissocoollike/uploads?alt=jsonc&v=2";
StringBuilder url = new StringBuilder(URL);
HttpGet get = new HttpGet(url.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONArray VideoData = new JSONArray(data);
JSONObject video = VideoData.getJSONObject(0);
return video;
}
我該如何從每個視頻對象的JSON數據中提取視頻ID,標題和描述?
謝謝你的回覆,但我會將這段代碼插入到getVideoJSON方法中,使其無效並移除getVideoData方法? –
這取決於你想要用它做什麼。我不知道你想用數據做什麼,但是這段代碼應該給你一個關於如何從所有視頻結果中獲得每個視頻標題和id(以及更多)的總體思路。 – smoak