从 YouTube API 解析 JSON

发布于 2024-12-08 07:27:01 字数 1386 浏览 0 评论 0原文

我有一个网址: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤千羽 2024-12-15 07:27:01

http://www.json.org/中有一个用JAVA实现的解析器。

In http://www.json.org/ has a parser implemented in JAVA.

凉世弥音 2024-12-15 07:27:01

你快到了。你需要的是:

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

然后你可以遍历每个视频:

for (int i = 0; i < items.length(); i++) {
    JSONObject videoObject = items.getJSONObject(i); 
    String title = videoObject.getString("title");
    String videoId = videoObject.getString("id");
}

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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文