从foursquare从JSONObject获取JSONarray时出错

时间:2013-10-08 17:55:09

标签: 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"
      },

我想在谷歌地图上显示场地,在此之前我试图解析这个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)

2 个答案:

答案 0 :(得分:10)

{ // 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);
        } 

答案 1 :(得分:0)

场地数组位于JSON的对象response中。您可以像这样访问它:

JSONObject object = new JSONObject(jsonResult); // The general object
JSONObject response = object.getJSONObject("response"); // The response object
venues = response.getJSONArray(TAG_VENUES); // The venues array