从YouTube API解析JSON [英] Parsing JSON from YouTube API
本文介绍了从YouTube API解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个网址: https:/ /gdata.youtube.com/feeds/api/users/charlieissocoollike/uploads?alt=jsonc&v=2 ,提供有关用户最新YouTube上传的JSON信息。
I have a URL: https://gdata.youtube.com/feeds/api/users/charlieissocoollike/uploads?alt=jsonc&v=2 which supplies JSON information on latest youtube uploads from a user.
我编写了一些代码来解析这个JSON数据,但我不明白JSON是如何工作的,以及如何用Java解析它。
I have written some code to parse this JSON data but I don't understand how JSON works and how to parse it in 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,标题和说明每个视频对象?
How should I pull the video id, title and description from the JSON Data of each video object?
推荐答案
你几乎就在那里。你需要的是:
You're almost there. What you need is:
JSONObject json = new JSONObject(data);
JSONObject dataObject = json.getJSONObject("data"); // this is the "data": { } part
JSONArray items = dataObject.getJSONArray("items"); // this is the "items: [ ] part
然后你可以遍历每个视频:
Then you can traverse over each video:
for (int i = 0; i < items.length(); i++) {
JSONObject videoObject = items.getJSONObject(i);
String title = videoObject.getString("title");
String videoId = videoObject.getString("id");
}
这篇关于从YouTube API解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文