我有一个网址: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、标题和描述?
最佳答案
在http://www.json.org/有一个用 JAVA 实现的解析器。
关于java - 从 YouTube API 解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7652791/