gpt4 book ai didi

java - 使用 Json 访问特定属性

转载 作者:太空狗 更新时间:2023-10-29 16:39:10 24 4
gpt4 key购买 nike

大家好,我正在使用 JAVA (Android) 阅读包含 JSON 对象的页面

页面内容如下:

{
"destination_addresses" : [ "Safi, Maroc" ],
"origin_addresses" : [ "Avenue Hassan II, Safi, Maroc" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "1,0 km",
"value" : 966
},
"duration" : {
"text" : "2 minutes",
"value" : 129
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}

我知道如何从页面读取内容

    JSONObject jArray = new JSONObject(result);
String elements = jArray.getString("rows");

元素字符串包含:

[{"elements" : [{"distance" : {"text" : "1,0 km","value" : 966},"duration" : {"text" : "2 minutes","value" : 129},"status" : "OK"}]}]

但我正在尝试获取距离值,即“966

谢谢大家

最佳答案

因此您正在尝试访问数组中的条目。请参阅教程或示例。这是基本的东西: http://www.mkyong.com/java/json-simple-example-read-and-write-json/

您需要实际使用 JSONArray (它是 JSONObject 的子类)

关于java - 使用 Json 访问特定属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21070770/

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