从 YouTube API 解析 JSON
我有一个网址:https://gdata。 youtube.com/feeds/api/users/charlieissocoollike/uploads?alt=jsonc&v=2 它提供有关用户最新 YouTube 上传的 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、标题和描述?
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.
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;
}
How should I pull the video id, title and description from the JSON Data of each video object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在http://www.json.org/中有一个用JAVA实现的解析器。
In http://www.json.org/ has a parser implemented in JAVA.
你快到了。你需要的是:
然后你可以遍历每个视频:
You're almost there. What you need is:
Then you can traverse over each video:
您最好使用官方客户端库:http://code .google.com/apis/youtube/2.0/developers_guide_java.html#Retriving_user_activity_feeds
You'd be better off using the official client library: http://code.google.com/apis/youtube/2.0/developers_guide_java.html#Retrieving_user_activity_feeds