android - 从 foursquare 的 JSONObject 获取 JSONarray 时出错

标签 android json

这是我的 JSON 响应,尝试解析此数据时出现 JSON 异常,如何解决此问题。

`{
  "meta": {
    "code": 200
  },
  "response": {
    "venues": [
      {
        "id": "4d090451c26ba14344c11c17",
        "name": "Hotel Grand Dhillon",
        "contact": {

        },
        "location": {
          "lat": 21.200844504492434,
          "lng": 81.32280155700252,
          "distance": 1457,
          "country": "India",
          "cc": "IN"
        },
        "categories": [
          {
            "id": "4bf58dd8d48988d1fa931735",
            "name": "Hotel",
            "pluralName": "Hotels",
            "shortName": "Hotel",
            "icon": {
              "prefix": "https:\/\/foursquare.com\/img\/categories_v2\/travel\/hotel_",
              "suffix": ".png"
            },
            "primary": true
          }
        ],
        "verified": false,
        "restricted": true,
        "stats": {
          "checkinsCount": 97,
          "usersCount": 55,
          "tipCount": 2
        },
        "specials": {
          "count": 0,
          "items": [

          ]
        },
        "hereNow": {
          "count": 0,
          "groups": [

          ]
        },
        "referralId": "v-1381236272"
      },
      {
        "id": "4d2155cf8629224ba2941a87",
        "name": "Hotel Bhilai Niwas",
        "contact": {

        },
        "location": {
          "lat": 21.178341816944222,
          "lng": 81.34224848671207,
          "distance": 4447,
          "city": "Bhilai",
          "state": "Chhattisgarh",
          "country": "India",
          "cc": "IN"
        },
        "categories": [
          {
            "id": "4bf58dd8d48988d1fa931735",
            "name": "Hotel",
            "pluralName": "Hotels",
            "shortName": "Hotel",
            "icon": {
              "prefix": "https:\/\/foursquare.com\/img\/categories_v2\/travel\/hotel_",
              "suffix": ".png"
            },
            "primary": true
          }
        ],
        "verified": false,
        "restricted": true,
        "stats": {
          "checkinsCount": 6,
          "usersCount": 6,
          "tipCount": 0
        },
        "specials": {
          "count": 0,
          "items": [

          ]
        },
        "hereNow": {
          "count": 0,
          "groups": [

          ]
        },
        "referralId": "v-1381236272"
      },

我想在谷歌地图上显示 field ,在此之前我试图解析这个 JSON 响应 这是我的代码

                jsonResult = inputStreamToString(
                    response.getEntity().getContent()).toString();

            JSONObject object = new JSONObject(jsonResult);
            venues = object.getJSONArray(TAG_VENUES);//getting exception here,cursor moving from here to catch block 
            ArrayList<HashMap<String, String>> venuesList = new ArrayList<HashMap<String, String>>();

            // looping through All Contacts
            for (int i = 0; i < venues.length(); i++) {
                JSONObject c = venues.getJSONObject(i);

                // Storing each json item in variable
                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);

                // Location is another JSONObject so
                JSONObject location = venues.getJSONObject(i);

                String lat = location.getString(TAG_LAT);
                String lon = location.getString(TAG_LON);
                String distance = location.getString(TAG_DISTANCE);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_ID, id);
                map.put(TAG_NAME, name);
                map.put(TAG_LAT, lat);
                map.put(TAG_LON, lon);
                map.put(TAG_DISTANCE, distance);

                // adding HashList to ArrayList
                venuesList.add(map);

在这里发布日志

    10-08 23:21:39.310: W/System.err(21249): org.json.JSONException: No value for venue
10-08 23:21:39.310: W/System.err(21249):    at org.json.JSONObject.get(JSONObject.java:354)
10-08 23:21:39.310: W/System.err(21249):    at org.json.JSONObject.getJSONArray(JSONObject.java:544)
10-08 23:21:39.310: W/System.err(21249):    at com.example.xxx.MainActivity$GetChildList.doInBackground(MainActivity.java:116)
10-08 23:21:39.310: W/System.err(21249):    at com.example.xxx.MainActivity$GetChildList.doInBackground(MainActivity.java:1)
10-08 23:21:39.310: W/System.err(21249):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-08 23:21:39.310: W/System.err(21249):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-08 23:21:39.310: W/System.err(21249):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-08 23:21:39.310: W/System.err(21249):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-08 23:21:39.310: W/System.err(21249):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-08 23:21:39.310: W/System.err(21249):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-08 23:21:39.320: W/System.err(21249):    at java.lang.Thread.run(Thread.java:856)

最佳答案

{ // json object node 
  "meta": {
    "code": 200
  },
  "response": { // response is a json object
    "venues": [ // venues is a json array

{代表json对象节点

[代表json数组节点

解析

JSONObject jb = new JSONObject("jsonstring");
JSONObject jb1 = jb.getJSONObject("response");
JSOnArray venues = jb1.getJSONArray("venues");

现在遍历json数组

{
    "meta": {
        "code": 200
    },
    "response": {
        "venues": [ // json array venues
            {      // json object node
                "id": "4d090451c26ba14344c11c17",
                "name": "Hotel Grand Dhillon",
                "contact": {},
                "location": {  // json object location with in json object node
                    "lat": 21.200844504492434,
                    "lng": 81.32280155700252,
                    "distance": 1457,
                    "country": "India",
                    "cc": "IN"
                }
            }
        ]
    }
}
 for(int i=0;i<venues.length();i++)
        {
            JSONObject b = venues.getJSONObject(i);
            String id = b.getString("id");
            Log.i(".......",id);
            JSONObject location = b.getJSONObject("location");
            String lat = location.getString("lat"); 
            Log.i(".......",lat);
        } 

关于android - 从 foursquare 的 JSONObject 获取 JSONarray 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19254853/

相关文章:

java - 将 HttpServletRequest 转换为 JSON 作为 "UTF-8"

android - 如何在不打开购买对话框的情况下验证用户已经在我的 Android 应用程序中完成购买?

android - 使用 Googlemaps API 将用户引导至某个位置 (Android)

python - 带有转义字符的 json.loads

php - FullCalendar 在月 View 中显示事件,但不在日、周或资源 View 中显示事件

javascript - 如何在前端JavaScript中访问Python(Flask)传递的数组(JSON)?

android - 如何获取Android中WiFi-Direct(WiFi-P2P)对等设备的IP地址?

android - 如何检测android中的用户存在?

android - 由于不同的应用程序签名,重新安装失败

ios - PFUser.current()?.saveInBackground 错误与 UIImage()