【问题标题】:Can't get value from JSON Data - YouTube API -无法从 JSON 数据中获取价值 - YouTube API -
【发布时间】:2017-02-13 00:35:49
【问题描述】:

我尝试从 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"
       }
      }
     ]
}

为什么我有 No value for 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"));
        }

【问题讨论】:

    标签: java android json youtube


    【解决方案1】:
    try
        {
            JSONObject mJsonObject = new JSONObject(strValue);
            JSONArray items = mJsonObject.getJSONArray("items");
            String nextToken = mJsonObject.getString("nextPageToken").toString();
    
            for(int i = 0;i<items.length();i++)
            {
    
                JSONObject snipetts = items.getJSONObject(i);
                JSONObject snipet = snipetts.getJSONObject("snippet");
                JSONObject thumbnails = snipet.getJSONObject("thumbnails");
                JSONObject highImage = thumbnails .getJSONObject("high");
    
                String title = snipet.get("title").toString();
                String urlImage = highImage.get("url").toString();
                String videoID = snipetts.getString("id");
                String description = snipet.getString("description");
                String publishDate = snipet.getString("publishedAt");
    
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    

    【讨论】:

      【解决方案2】:

      使用此代码:

       JSONObject json = new JSONObject(jsonText);
      
      
                  List<String> list = new ArrayList<String>();
                  JSONArray array = null;
                  try {
                      array = json.getJSONArray("items");
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
                  for(int i = 0 ; i < array.length() ; i++){
                      // list.add(array.getJSONObject(i).getString("contentDetails"));
                      try {
                          System.out.println(array.getJSONObject(i).getJSONObject("contentDetails").getString("videoId"));
                      } catch (JSONException e) {
                          e.printStackTrace();
                      }
                  }
      

      【讨论】:

        【解决方案3】:

        因为videoIditems 对象下。 先获取item,然后获取它的first child,然后获取videoId

        编辑:使用

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

        并删除println

        然后在循环之外只打印包含videoIds 的列表

        【讨论】:

        • 好的,所以videoIDcontentDetails 谁在items 中?我怎样才能先得到item
        • 找到getObject和getArray方法并使用它们
        • 我已经编辑了我的第一篇文章,我尝试了 getArray 方法,但也没有用
        猜你喜欢
        • 2021-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-13
        • 2018-10-08
        • 1970-01-01
        • 1970-01-01
        • 2021-05-21
        相关资源
        最近更新 更多