尝试使用YouTube API从json提取视频ID

时间:2019-04-02 04:05:09

标签: android json youtube-api

我正在尝试从json中提取“ videoid”(是字符串)     从youtube api获得并希望将该“ videoid”提供给另一个我已尝试调用player.loadVideo(“ Videoid”)的.java文件  提取它,但返回null

{
 "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”随后可以使用     在player.java中,通过player.loadVideo(“ data”)函数

1 个答案:

答案 0 :(得分:3)

假设此响应有一个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"); 
    }