【问题标题】:How do i parse Google Places Detail JSON file?如何解析 Google Places Detail JSON 文件?
【发布时间】:2012-08-31 17:25:23
【问题描述】:

我是 android 的菜鸟,我想用与此处示例相同的格式解析 google 响应 JSON:https://developers.google.com/places/documentation/details。我正在尝试获取字符串格式的电话号码。但是,当我从数组“事件”编码到 .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());
  }      

【问题讨论】:

  • 如果我理解正确,因为它是一个数组,它可能包含多个电话号码。如果您确定只有一个电话号码,请尝试 0 index

标签: android json string parsing int


【解决方案1】:

formatted_phone_numberresults json 对象中,而不是在events json 数组中。

换行:

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

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

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

【讨论】:

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