我正在尝试从json中提取"videoid"(这是一个字符串( 从youtube API获得,并希望将该"videoid"提供给另一个调用player.loadVideo("Videoid"(的.java文件,我已经尝试过 提取它,但其返回空
值{
"kind": "youtube#searchListResponse",
"etag": ""XpPGQXPnxQJhLgs6enD_n8JR4Qk/twPDbB5irYgF-0BnQZIeukjX7k0"",
"regionCode": "CA",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#searchResult",
"etag": ""XpPGQXPnxQJhLgs6enD_n8JR4Qk/SmOmHZrRW-QMb9RisiuAxWDkXao"",
"id": {
"kind": "youtube#video",
<b> "videoId": "8ykDBg-b4QE"<b>
},
"snippet": {
"publishedAt": "2019-03-30T11:43:48.000Z",
"channelId": "UCk4o4gWFuU2YTS50ym4RbXg",
"title": "Streaming...",
"description": "Become a sponsor: xPfsD Multistreaming with ream.io/
veraise.com/channel/622995 Donate: ...",
"thumbnails": {
"default": {
"url": "vi/8ykDBg-b4QE/default_live.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": ".ytimg.com/vi/8ykDBg-b4QE/mqdefault_live.jpg",
"width": 320,
"height": 180
},
"high": {
"url": ".ytimg.com/vi/8ykDBg-b4QE/hqdefault_live.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Heavenly Controller",
"liveBroadcastContent": "live"
}
}
]
}
JSONArray JA = new JSONArray(data);
for(int i =0 ;i <JA.length(); i++){
JSONObject JO = (JSONObject) JA.get(i);
//singleParsed = //"Name:" + JO.get("name") + "n"+
// JO.get("videoId") + "n"
//"Contact:" + JO.get("contact") + "n"+
// "Country:" + JO.get("country") + "n"
// ;
// dataParsed = dataParsed + singleParsed +"n" ;
System.out.println(JO);
基本上期望值数据="8ykDBg-b4QE",然后可以使用 内部播放器.java由函数 player.loadVideo("data"(
假设我们有一个用于此响应的 jsonObject。现在,根据您的 json 响应,以下代码将获取 videoId:
JSONArray jArr = jsonObject.getJSONArray("items");
for(int i =0; i < jArr.length(); i++)
{
// getting object from items array
JSONObject itemObj = jArr.getJSONObject(i);
// getting id object from item object
JSONObject idObj = itemObj.getJSONObject("id");
// getting videoId from idObject
String videoId = idObj.getString("videoId");
}