如何解析Google Places Detail JSON文件?

时间:2012-09-06 20:17:39

标签: android json string parsing int

我是Android的菜鸟,我想用与此处示例相同的格式解析谷歌回复JSON:https://developers.google.com/places/documentation/details。我正在尝试获取字符串格式的电话号码。但是,当我从数组“events”中编码.getString时,我的编辑器要求输入一个int索引。我不明白发生了什么。任何帮助是极大的赞赏。这是我的代码:

try {   
        JSONObject googleObject = StoresNearMe.getJSONfromURL(googleplacedetailapi);
        JSONObject parsedGoogle = googleObject.getJSONObject("results");
        JSONArray  parsedevents = parsedGoogle.getJSONArray("events");
        //thing.setText(googleplacesapi);
        if(parsedevents.equals("") ){
            Toast.makeText(FindDealer.this, "No nearby locations", Toast.LENGTH_LONG).show();
        }else{
            String phonenumber = parsedevents.getString("formatted_phone_number");
            thing.setText(phonenumber);   
      }            
  } catch (JSONException e) {
      Log.d("log_tag","JSON parsing error - Google Places Api:" + e.getMessage());
  }      

1 个答案:

答案 0 :(得分:1)

formatted_phone_number位于results json对象中,而不在events json数组中。

更改行:

String phonenumber = parsedevents.getString("formatted_phone_number");

String phonenumber = parsedGoogle.getString("formatted_phone_number");

您可以在json.org上阅读有关json结构的内容。