gpt4 book ai didi

java - 无法从 JSON 数据获取值 - YouTube API -

转载 作者:太空宇宙 更新时间:2023-11-04 12:11:05 25 4
gpt4 key购买 nike

我尝试从 json 数据(YouTube V3 API)获取值。

String jsonText = readFromUrl(channerUrl);// channerUrl = Youtube V3 API Link

JSONObject json = new JSONObject(jsonText);

System.out.println(json.getString("videoId"));

但是我的日志中有这个:

I/System.out: org.json.JSONException: No value for videoId

来自 YouTube 的数据是:

{
"kind": "youtube#playlistItemListResponse",
"etag": "\"xxxx"",
"nextPageToken": "xxx",
"pageInfo": {
"totalResults": 321,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#playlistItem",
"etag": "\"xxxx"",
"id": "xxxx",
"contentDetails": {
"videoId": "The Value Want"
}
}
]
}

为什么我的videoId没有值?谢谢

编辑(也不起作用): (jsonText) 是 JSON 数据的链接

 JSONObject json = new JSONObject(jsonText);


List<String> list = new ArrayList<String>();
JSONArray array = json.getJSONArray("items");
for(int i = 0 ; i < array.length() ; i++){
list.add(array.getJSONObject(i).getString("contentDetails"));
System.out.println(json.getString("contentDetails"));
}

最佳答案

因为 videoId 位于 items 对象下。首先获取item,然后获取其第一个子项,然后获取videoId

编辑:使用

array.getJSONObject(i).getJSONObject("contentDetails").getString("videoId")

并删除 println

然后在循环外部打印包含 videoId 的列表

关于java - 无法从 JSON 数据获取值 - YouTube API -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39859165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com