使用我正在访问的API的以下JSON输出,如何访问长城市名称"西南波特兰"?它嵌套在对象和数组中如此之深,以至于我遇到了麻烦。
JSON:
{
"results" : [
{
"address_components" : [
{
"long_name" : "3229-3353",
"short_name" : "3229-3353",
"types" : [ "street_number" ]
},
{
"long_name" : "Southwest Dolph Court",
"short_name" : "SW Dolph Ct",
"types" : [ "route" ]
},
{
"long_name" : "Southwest Portland",
"short_name" : "Southwest Portland",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Portland",
"short_name" : "Portland",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Multnomah County",
"short_name" : "Multnomah County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Oregon",
"short_name" : "OR",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "97219",
"short_name" : "97219",
"types" : [ "postal_code" ]
}
],
以下是我在15以下编码的尝试之一。每一次,我都会得到不同的东西。我非常接近,我只是无法弄清楚如何只提取邻居名称,因为每个地址组件的标题中都有long_name和short_name。谢谢您的帮助!
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
String message = "";
JSONObject jsonObject = new JSONObject(result);
String JSONresults = jsonObject.getString("results");
Log.i("JSON content", JSONresults);
JSONArray arr = new JSONArray(JSONresults);
for (int i = 0; i < arr.length(); i++) {
JSONObject jsonPart = arr.getJSONObject(i);
String neighborhoodLong = "";
String neighborhoodShort = "";
neighborhoodLong = jsonPart.getString("long_name");
neighborhoodShort = jsonPart.getString("short_name");
if (neighborhoodLong != "" && neighborhoodShort != "") {
message += neighborhoodLong + ": " + neighborhoodShort;
}
}
if (message != "") {
Log.i("neigh", message);
//insert succesful code here
} else {
//unsuccesful code goes here
}
} catch (JSONException e) {
//error code here
}
}
答案 0 :(得分:1)
你有一个数组数组。但是你只使用了1 for loop
这是不够的。
你需要进入第一个数组并迭代另一个数组 -
for (int i = 0; i < arr.length(); i++) {
JSONArray internalArray = arr.getJSONObject(i).getJSONArray("address_components");
for (int index = 0; index < internalArray.length(); index++) {
String neighborhoodLong = "";
String neighborhoodShort = "";
JSONObject jsonPart = internalArray.getJSONObject(index);
JSONArray typeArray = jsonPart.getJSONArray("types");
for(int counter =0 ; counter< typeArray.length(); counter++){
if(typeArray.getString(counter).equalsIgnoreCase("neighborhood")){
neighborhoodLong = jsonPart.getString("long_name");
neighborhoodShort = jsonPart.getString("short_name");
if (!TextUtils.isEmpty(neighborhoodLong) && !TextUtils.isEmpty(neighborhoodShort)) {
message += neighborhoodLong + ": " + neighborhoodShort;
}
break;
}
}
}
if (!TextUtils.isEmpty(message)) {
Log.i("neigh", message);
//insert succesful code here
} else {
//unsuccesful code goes here
}
} catch (JSONException e) {
//error code here
}
希望这会有所帮助!!
答案 1 :(得分:1)
你走了。我已经很好地评论了代码,以便您理解。
try
{
//This is the whole thing
JSONObject Jresult = new JSONObject(result);
//Now we take the JSONarray results
JSONArray Results = Jresult.getJSONArray("results");
//IMPORTANT
//Now we have to take the object in the array.
//Since there is only 1 obj. You dont need a for loop
JSONObject ResObj = Results.getJSONObject(1);
//Now we get the array address_components
JSONArray addressComps = ResObj.getJSONArray("address_components");
//Time to get the obj we want from the array
//Since u mentioned only thst one name and provided the result
//I knew the number in the array. In this case 3.
JSONObject needed = addressComps.getJSONObject(3);
//Now we get the required string
String longName = needed.getString("long_name");
}
catch (JSONException e)
{}
需要注意的一些重要事项。 我没有使用for循环,因为我们知道对象在数组中的位置。但是如果你在另一种情况下处理它,你可以使用for循环。你已经知道了。
此外,我可能有json的字符串名称错误。你知道,识别名称。所以请仔细检查以防万一。我希望这可以帮助你!
答案 2 :(得分:0)
使用Androids TextUtils,它检查String是空还是空。
尝试替换:
if (neighborhoodLong != "" && neighborhoodShort != "") {
message += neighborhoodLong + ": " + neighborhoodShort;
}
if (message != "") {
...
}
使用:
if (!TextUtils.isEmpty(neighborhoodLong) && !TextUtils.isEmpty(neighborhoodShort)) {
message += neighborhoodLong + ": " + neighborhoodShort;
}
if (!TextUtils.isEmpty(message)) {
...
}
此外,您似乎无法正确解析。 &#34; address_components&#34;是一个json数组,所以这里有两个数组。在for循环中尝试:
JSONObject jsonPart = arr.getJSONObject(i);
JSONArray address_components = jsonPart.getJsonArray("address_components");
希望有所帮助!