【问题标题】:Parsing JSON response in Android在 Android 中解析 JSON 响应
【发布时间】:2010-11-29 19:43:09
【问题描述】:

我正在为 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 和 geolong 值?

【问题讨论】:

    标签: android json foursquare


    【解决方案1】:

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

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

    【讨论】:

    • 这会返回错误“Geolat 没有值”是因为它在场地内吗?
    • @Christina: 嗯,是的"geolat" 在场地内(根据你的例子)。这就是为什么我在我的代码中首先获取一个代表场地的JSONObject(来自JSONArray venues),然后从venueObject 获取纬度和经度。确保拼写正确,并在调试器中单步调试代码以更详细地检查venueObject 的值(如果需要)。
    • 刚刚注意到组周围的 { 没有复制。我认为这就是为什么它不允许我访问 geolat?我现在已经编辑了 Q。感谢您的帮助
    【解决方案2】:

    您可以检查 here 并选择一个 JSON 库来获取这些值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多