2

これが私の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"
      },

Googleマップに会場を表示したいのですが、その前に、この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)
4

2 に答える 2

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);
        } 
于 2013-10-08T18:02:02.690 に答える
0

会場配列はresponse、JSON のオブジェクトにあります。次のようにアクセスできます。

JSONObject object = new JSONObject(jsonResult); // The general object
JSONObject response = object.getJSONObject("response"); // The response object
venues = response.getJSONArray(TAG_VENUES); // The venues array
于 2013-10-08T18:03:20.347 に答える