解析Android中的JSON响应

时间:2010-11-29 19:43:09

标签: android json foursquare

我正在为android编写一个httpget请求,查询附近事件的foursquare api。我收到的JSON响应是

{"groups": [
{
  "type": "Nearby",
  "venues": [
    {
      "id": 2587838,
      "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
    },

这些场地可能很多。我可以使用此代码打印出场地信息

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();

如何访问geolat&amp; amp; geolong值?

2 个答案:

答案 0 :(得分:12)

我相信将此代码放在for循环中应该可以解决问题:

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

答案 1 :(得分:0)

您可以查看here并选择要用于获取这些值的JSON库。