gpt4 book ai didi

java - 如何从嵌套数组中提取 JSON 数据?使用安卓

转载 作者:行者123 更新时间:2023-11-30 08:56:56 24 4
gpt4 key购买 nike

这是我的 JSON 结果,我需要在持续时间数组中获取 text。如何获取?

{
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 19.0759817,
"lng" : 80.27071789999999
},
"southwest" : {
"lat" : 12.5100381,
"lng" : 72.8776544
}
},
"copyrights" : "Map data ©2015 Google",
"legs" : [
{
"distance" : {
"text" : "1,334 km",
"value" : 1333618
},
"duration" : {
"text" : "17 hours 43 mins",
"value" : 63762
},
"end_address" : "Mumbai, Maharashtra, India",
"end_location" : {
"lat" : 19.0759817,
"lng" : 72.8776544
},
"start_address" : "Chennai, Tamil Nadu, India",
"start_location" : {
"lat" : 13.0826782,
"lng" : 80.27071789999999
},

我已经尝试使用这段代码,所以我可以获得整个 distance 数组,但我只需要其中的 text 元素。

  JSONArray one = reader.getJSONArray("routes");

for (int i = 0; i < one.length(); i++) {

JSONObject two = one.getJSONObject(i);

JSONArray three = two.getJSONArray("legs");
for (int j = 0; j < three.length(); j++) {

JSONObject four = three.getJSONObject(j);

String str_long_name=four.getString("distance");

}

}

所以我尝试像这样修改代码

 JSONArray one = reader.getJSONArray("routes");

for (int i = 0; i < one.length(); i++) {

JSONObject two = one.getJSONObject(i);

JSONArray three = two.getJSONArray("legs");
for (int j = 0; j < three.length(); j++) {

JSONObject four = three.getJSONObject(j);
JSONArray five = four.getJSONArray("distance");
for (int k= 0; k < five.length(); k++) {

JSONObject six = five.getJSONObject(k);
String str_long_name=six.getString("text");

}

}

}

但它不起作用。它也没有显示任何错误。

最佳答案

JSONArray five = four.getJSONArray("distance"); 是错误的。 “距离” 是一个 JSONObject

试试下面的代码。

JSONObject five = four.getJSONObject("distance");
String str_long_name=five.getString("text");
System.out.println(str_long_name);

这里是完整的测试代码

import org.json.JSONArray;
import org.json.JSONObject;

public class Test {

public static void main(String[] args) {
String str = "{\"routes\":[{\"bounds\":{\"northeast\":{\"lat\":19.0759817,\"lng\":80.27071789999999},\"southwest\":{\"lat\":12.5100381,\"lng\":72.8776544}},\"copyrights\":\"Map data ©2015 Google\",\"legs\":[{\"distance\":{\"text\":\"1,334 km\",\"value\":1333618},\"duration\":{\"text\":\"17 hours 43 mins\",\"value\":63762},\"end_address\":\"Mumbai, Maharashtra, India\",\"end_location\":{\"lat\":19.0759817,\"lng\":72.8776544},\"start_address\":\"Chennai, Tamil Nadu, India\",\"start_location\":{\"lat\":13.0826782,\"lng\":80.27071789999999}}]}]}";
JSONObject reader = new JSONObject(str);
JSONArray one = reader.getJSONArray("routes");

for (int i = 0; i < one.length(); i++) {

JSONObject two = one.getJSONObject(i);

JSONArray three = two.getJSONArray("legs");
for (int j = 0; j < three.length(); j++) {

JSONObject four = three.getJSONObject(j);
JSONObject five = four.getJSONObject("distance");
String str_long_name=five.getString("text");
System.out.println(str_long_name);

}

}
}
}

关于java - 如何从嵌套数组中提取 JSON 数据?使用安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28364280/

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