gpt4 book ai didi

java - 需要帮助从完整的 JSON 数组/对象中提取字符串

转载 作者:行者123 更新时间:2023-11-29 20:00:54 25 4
gpt4 key购买 nike

使用我正在访问的 API 的以下 JSON 输出,如何访问长城市名称“Southwest Portland”?它在对象和数组中嵌套得很深,我遇到了麻烦。

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
}



}

最佳答案

给你。我已经对代码进行了很好的注释,以便您理解。

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 内容的字符串名称。你知道,识别名称。因此,请仔细检查以防万一。希望对您有所帮助!

关于java - 需要帮助从完整的 JSON 数组/对象中提取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36294994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com