麻烦解析YouTube v3 JSON响应

时间:2015-09-19 05:15:38

标签: json android-youtube-api

我在解析来自YouTube的JSON响应方面遇到了一些麻烦,但我需要一些帮助。我正在尝试获取字段:id,videoId,缩略图和网址 - 使用以下内容:

private String getUrl(String id) throws IOException, JSONException {
    HttpClient client = new DefaultHttpClient();
    HttpGet clientGetMethod = new HttpGet(YOUTUBE_INFO_URL.replace("_ID_", id));
    HttpResponse clientResponse = null;
    clientResponse = client.execute(clientGetMethod);
    String infoString = _convertStreamToString(clientResponse.getEntity().getContent());
    String urldata=new JSONObject(infoString).getJSONObject("id").getJSONObject("videoId").getJSONArray("thumbnails").getJSONObject(0).getString("url");
    return new JSONObject(infoString).getJSONObject("id").getJSONObject("videoId").getJSONArray("thumbnails").getJSONObject(0).getString("url");
}

YouTube API v3回复: http://pastebin.com/LKWC2Cbz

然而,由于解析不正确,我不断遇到致命错误。有人可以找到我可能出错的地方吗?我正在指定我需要的字段 - 但是我觉得结构必须以某种方式与JSON响应不匹配。

1 个答案:

答案 0 :(得分:2)

所以我假设infoString包含您粘贴到共享的pastebin链接的JSON响应。

这是我的第一个Java程序,我正在使用Java 8,所以事情与你的代码有点不同(我使用的是JsonObject,而不是JSONObject,虽然在阅读程序时应该清楚你是什么的需要修改)

param["some-key"]

该程序的输出是:

package stackoverflowyoutubejson;

import java.io.IOException;
import javax.json.*;

public class StackOverflowYoutubeJson {
    public static void main(String[] args) {
        try {
            JsonObject object;
            // Instead of making a Http GET request I just read out the JSON file's contents saved down locally
            try (JsonReader jsonReader = Json.createReader(StackOverflowYoutubeJson.class.getResourceAsStream("input/youtube-response.json"))) {
                object = jsonReader.readObject();
            }

            // Obtaining the items array
            JsonArray items = object.getJsonArray("items");

            // Iterating the items array
            for(int i = 0; i < items.size(); i++) {
                JsonObject item = items.getJsonObject(i);
                JsonString id = item.getJsonString("id");
                System.out.println("id: " + id);

                JsonObject snippet = item.getJsonObject("snippet");
                JsonString videoId = snippet.getJsonObject("resourceId").getJsonString("videoId");
                System.out.println("videoid: " + videoId);

                JsonString url = snippet.getJsonObject("thumbnails").getJsonObject("default").getJsonString("url");
                System.out.println("default thumbnail url: " + url);
                System.out.println();
            }
        }
        catch(IOException e) {
            System.out.println(e);
        }
    }
}

所以基本上,如果你有这样的JSON:

id: "PL7ztmfZ6VHFYVLwdKgxI9lwOWFV_yoCFOK5o9K8KSE2s"
videoid: "PcfLDmkpzto"
default thumbnail url: "https://i.ytimg.com/vi/PcfLDmkpzto/default.jpg"

id: "PL7ztmfZ6VHFYVLwdKgxI9l7VIO-rUMLOT7pjiYSbTRPw"
videoid: "D9ohtWGSl9M"
default thumbnail url: "https://i.ytimg.com/vi/D9ohtWGSl9M/default.jpg"

id: "PL7ztmfZ6VHFYVLwdKgxI9l5gWA-vAfTbxQVrUWaMILLA"
videoid: "B1OluIUHLnY"
default thumbnail url: "https://i.ytimg.com/vi/B1OluIUHLnY/default.jpg"

id: "PL7ztmfZ6VHFYVLwdKgxI9l9A2H_-9HSzVlvT--kLf0TA"
videoid: "LjKpcUJSjtM"
default thumbnail url: "https://i.ytimg.com/vi/LjKpcUJSjtM/default.jpg"

id: "PL7ztmfZ6VHFYVLwdKgxI9l9nWIbKA-8Bnu3v_D6xEKaU"
videoid: "fTSmcQdLyhU"
default thumbnail url: "https://i.ytimg.com/vi/fTSmcQdLyhU/default.jpg"

您可以访问以下字段:

{
    "field1": {
        "nestedField1": "nestedValue1",
        "nestedField2": "nestedValue2"
    },
    "field2": "value2"
}