Android不确定如何引用JSON文件中的值(JSON解析)

Android不确定如何引用JSON文件中的值(JSON解析),android,json,android-location,arrays,jsonobject,Android,Json,Android Location,Arrays,Jsonobject,JSON页面示例 此代码正确返回地址。如何获取地址组件中的邮政编码长名称 解决方案 我必须得到每个数组,然后得到post-code值。 我使用的是值7,因为它是JSONObject,其邮政编码存储在long_name字段中 JSONObject readerJsonObject = new JSONObject(in); readerJsonObject.getJSONArray("results"); JSONArray resultsJsonArray = readerJsonObje

JSON页面示例

此代码正确返回地址。如何获取地址组件中的邮政编码长名称

解决方案 我必须得到每个数组,然后得到post-code值。 我使用的是值7,因为它是JSONObject,其邮政编码存储在long_name字段中

JSONObject readerJsonObject = new JSONObject(in);
 readerJsonObject.getJSONArray("results");
 JSONArray resultsJsonArray = readerJsonObject.getJSONArray("results");
 JSONArray postCodeJsonArray = resultsJsonArray.getJSONObject(0).getJSONArray("address_components");
 String postCodeString =  postCodeJsonArray.getJSONObject(7).getString("long_name").toString();         
 Log.e("TAG", postCodeString);
JSONObject readerJsonObject = new JSONObject(in);
 readerJsonObject.getJSONArray("results");
 JSONArray resultsJsonArray = readerJsonObject.getJSONArray("results");
 JSONArray postCodeJsonArray = resultsJsonArray.getJSONObject(0).getJSONArray("address_components");
 String postCodeString =  postCodeJsonArray.getJSONObject(7).getString("long_name").toString();         
 Log.e("TAG", postCodeString);
希望能有所帮助。

您的问题是,结果是一个JSONArray,其中包含一个子JSONObject,它由几个子对象组成:地址组件、格式化地址、几何体和类型。结果数组实际上包含许多这样的对象,但现在让我们只关注第一个子对象

reader.getJSONObject(1).getJSONArray("address_components");
仔细查看您的代码。这一行:

JSONArray resultArry = reader.getJSONArray("results");
你得到了全部结果。稍后,再次调用相同的方法:

JSONArray postCodeArray  = reader.getJSONArray("address_components");
但是你要求读者提供一个地址,我不希望你会发现之前已经阅读了整个结果的任何东西。您应该使用之前得到的JSONArray,因为它已经包含了整个结果

尝试以下方法:

JSONObject addressComponents = resultArry.getJSONObject(1).getJSONObject("address_components");
String postCode = addressComponents.getString("long_name");

注意:我不知道为什么要选择JSONObject 1而不是0,0是第一个,或者其他任何一个,我也不知道为什么要将字符串命名为postCode。因此,如果我误解了您的意图,我道歉。

很难找到错误。。。因为一切看起来都很好。当您制作json.putaddress_组件时,问题可能会存在

所以我的建议是在这一行设置一个断点

 JSONArray postCodeArray  = reader.getJSONArray("address_components");
o在logcat中显示json

Log.d("Simple", reader.toString());
然后将json粘贴到此网页中,以查看更漂亮的内容

并检查所有钥匙是否存放完好。

解决方案 需要获取每个数组,然后获取post-code值。使用值7,因为它是JSONObject,其邮政编码存储在long_name字段中

JSONObject readerJsonObject = new JSONObject(in);
 readerJsonObject.getJSONArray("results");
 JSONArray resultsJsonArray = readerJsonObject.getJSONArray("results");
 JSONArray postCodeJsonArray = resultsJsonArray.getJSONObject(0).getJSONArray("address_components");
 String postCodeString =  postCodeJsonArray.getJSONObject(7).getString("long_name").toString();         
 Log.e("TAG", postCodeString);
JSONObject readerJsonObject = new JSONObject(in);
 readerJsonObject.getJSONArray("results");
 JSONArray resultsJsonArray = readerJsonObject.getJSONArray("results");
 JSONArray postCodeJsonArray = resultsJsonArray.getJSONObject(0).getJSONArray("address_components");
 String postCodeString =  postCodeJsonArray.getJSONObject(7).getString("long_name").toString();         
 Log.e("TAG", postCodeString);

希望这能有所帮助。

嗨,我挑出了JsonObject 1,因为它以我想要的格式返回了地址。我将字符串命名为postCode,因为我试图只保存JSON中的postCode部分。地址部分工作正常,我从JSON文件中获取了地址,我只是尝试单独获取邮政编码以存储到邮政编码字符串中。我尝试了您的代码,在地址处发现错误,类型为org.JSON.JSONArray的组件无法转换为org.JSON.JSON.typemistmatchjson.java:100Yup的JSONObject,看起来您最终得到了它。JSON对[]和{}很棘手,我似乎总是把它们搞混。谢谢你的建议。我已经解决了,请查看我的更新答案。谢谢你的评论。我解决了这个问题。请查看我的答案以获取更新。如果这是解决方案,您应该将其标记为答案,以便后面的每个人都清楚。
reader.getJSONObject(1).getJSONArray("address_components");