开发者

Parsing JSON response in Android

开发者 https://www.devze.com 2023-01-28 00:47 出处:网络
I am writing a httpget request for android that queries the foursquare api for nearby event. The JSON responce I receive back is

I am writing a httpget request for android that queries the foursquare api for nearby event. The JSON responce I receive back is

{"groups": [
{
  "type": "Nearby",
  "venues": [
    {
      "id": 258开发者_StackOverflow社区7838,
      "name": "Marriott Druids Glen Hotel Newtownmountkennedy",
      "primarycategory": {
        "id": 79281,
        "fullpathname": "Travel:Resort",
        "nodename": "Resort",
        "iconurl": "http://foursquare.com/img/categories/travel/resort.png"
      },
      "address": "Newtownmountkennedy",
      "city": "Newtownmountkennedy",
      "state": "",
      "verified": false,
      "geolat": 53.091717,
      "geolong": -6.079162,
      "stats": {
        "herenow": "0"
      },
      "distance": 5596
    },

There can be many of these venues. I can get it to print out the venues information using this code

InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
Log.i("Hoosheer0",result);

// A Simple JSONObject Creation
JSONObject json=new JSONObject(result);
JSONArray venues = json.getJSONArray("groups");
//JSONArray docsArray = jObject.getJSONArray("docs");
for (int i = 0; i<venues.length();i++){
    String name = venues.getJSONObject(i).optString("venues");
    //String venueName = name.
    Log.i("This is a name... pleasse", name);
}

instream.close();

How can I access the geolat & geolong values?


I believe placing this code inside your for-loop should do the trick:

JSONObject venueObject = venues.getJSONObject(i);
double geoLat = venueObject.getDouble("geolat");
double geoLong = venueObject.getDouble("geolong");


You can check here and pick a JSON library to use to get at those values.

0

精彩评论

暂无评论...
验证码 换一张
取 消