【问题标题】:How to get & parse a JSON object in Android? [duplicate]如何在 Android 中获取和解析 JSON 对象? [复制]
【发布时间】:2012-11-02 09:14:17
【问题描述】:

可能重复:
How do I parse JSON from a Java HTTPResponse?
How to parse json string in Android?

如何解析这个json字符串

{
"apiVersion": "2.1",
"data": {
    "id": "pHuoDqcIyqk",
    "uploaded": "2012-10-29T16:08:15.000Z",
    "updated": "2012-11-02T08:48:28.000Z",
    "uploader": "googlenexus",
    "category": "Tech",
    "title": "Nexus: Ask Me Anything",
    "description": "The Best of Google, now in 3 sizes. Introducing Nexus 4, Nexus 7 and Nexus 10. The new smartphone and tablets from Google. Shop now at play.google.com/nexus",
    "thumbnail": {
        "sqDefault": "http://i.ytimg.com/vi/pHuoDqcIyqk/default.jpg",
        "hqDefault": "http://i.ytimg.com/vi/pHuoDqcIyqk/hqdefault.jpg"
    },
    "player": {
        "default": "http://www.youtube.com/watch?v=pHuoDqcIyqk&feature=youtube_gdata_player",
        "mobile": "http://m.youtube.com/details?v=pHuoDqcIyqk"
    },
    "content": {
        "5": "http://www.youtube.com/v/pHuoDqcIyqk?version=3&f=videos&app=youtube_gdata",
        "1": "rtsp://v8.cache5.c.youtube.com/CiILENy73wIaGQmpyginDqh7pBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
        "6": "rtsp://v7.cache4.c.youtube.com/CiILENy73wIaGQmpyginDqh7pBMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"
    },
    "duration": 61,
    "aspectRatio": "widescreen",
    "rating": 4.8985643,
    "likeCount": "5227",
    "ratingCount": 5363,
    "viewCount": 1038854,
    "favoriteCount": 0,
    "commentCount": 1442,
    "accessControl": {
        "comment": "allowed",
        "commentVote": "allowed",
        "videoRespond": "moderated",
        "rate": "allowed",
        "embed": "allowed",
        "list": "allowed",
        "autoPlay": "allowed",
        "syndicate": "allowed"
    }
}
}

谁能告诉我如何调用上述 URL 并获取 JSON 对象,然后解析它以获取所需的字符串信息?

【问题讨论】:

标签: android json


【解决方案1】:

你问的问题太笼统了..

1) 对 URL 执行 http get 请求。 (或使用this 方法)并获取响应字符串,即JSON。一些例子here

2) 请参阅this 回答以了解如何解析 JSON。

【讨论】:

    【解决方案2】:

    简单简单....

    String str = "" // your JSON string
    JSONObject json = new JSONObject(str);
    String apiVersion = json.getString("apiVersion");
     // continue as before for the rest of it...
    

    【讨论】:

      【解决方案3】:

      你可以试试下面的方法。您可以传递 URL 并将响应作为 result 字符串。 公共静态字符串结果;

      public static boolean connect(String url) {
              boolean flag = false;
              HttpClient httpclient = new DefaultHttpClient();
      
              // Prepare a request object
              HttpGet httppost = new HttpGet(url);
              Log.e("url", url);
      
              // Execute the request
              HttpResponse response;
              try {
      
                  // https://api.vkontakte.ru/method/audio.search?uid=163398985&q=akoncount=100&access_token=2a4db0e223f0f5ab23f0f5ab5f23da5680223f023f1f5a3c696b018be9b17b9
      
                  response = httpclient.execute(httppost);
                  // Examine the response status
                  Log.i("vkontake", response.getStatusLine().toString() + "\n"
                          + response);
      
                  // Get hold of the response entity
                  HttpEntity entity = response.getEntity();
                  // If the response does not enclose an entity, there is no need
                  // to worry about connection release
      
                  if (entity != null) {
      
                      // A Simple JSON Response Read
                      InputStream instream = entity.getContent();
                      Log.d("Jsomn Activity", "---- is --- " + instream);
                      result = convertStreamToString(instream);
                      Log.d("Jsomn Activity", "---- Result --- " + result);
      
                      // now you have the string representation of the HTML request
                      instream.close();
      
                  } else {
                      Log.d("Jsomn Activity", "---- is --- null ");
                  }
                  flag = true;
                  net = false;
              } catch (Exception e) {
                  Log.d("Jsomn Activity", "---- Catch --- " + e.toString());
                  flag = false;
                  e.printStackTrace();
              } finally {
                  return flag;
              }
      
          }
      

      【讨论】:

        【解决方案4】:

        有几种方法可以做到这一点。一种简单的方法是定义一个与您的数据匹配的对象结构并使用http://code.google.com/p/google-gson/ 来填充它:

        final URL url = new URL("http://yourURL.com");
        final InputStream openStream = url.openStream();
        final InputStreamReader inputStreamReader = new InputStreamReader(openStream);
        final SearchResult searchResult = new Gson().fromJson(inputStreamReader, SearchResult.class);
        

        如果您的 searchResult 类包含一个字段,例如apiVersion,它将填充来自 JSON 的数据。然后你有一个数据字段,里面有其他字段等。这是填充数据的一种非常简单的方法,但你必须在对象模型中镜像 JSON 流的结构。

        【讨论】:

          【解决方案5】:

          JSON.org site 包含不少于 20 种不同的 JSON 解析器 java 实现。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-07-30
            • 1970-01-01
            • 2017-04-08
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多