Java 获取包含JSON数组的JSON对象的值

Java 获取包含JSON数组的JSON对象的值,java,arrays,json,Java,Arrays,Json,如何使用java从以下JSON对象中获取“距离”的值 { "destination_addresses" : [ "New York City, New York, Verenigde Staten" ], "origin_addresses" : [ "Washington D.C., District of Columbia, Verenigde Staten" ], "rows" : [ { "elements" : [ {

如何使用java从以下JSON对象中获取“距离”的值

{
  "destination_addresses" : [ "New York City, New York, Verenigde Staten" ],
  "origin_addresses" : [ "Washington D.C., District of Columbia, Verenigde Staten" ],
  "rows" : [
    {
       "elements" : [
          {
             "distance" : {
                "text" : "225 mijl",
                "value" : 361714
             },
             "duration" : {
                "text" : "3 uur 51 min.",
                "value" : 13877
             },
             "status" : "OK"
          }
       ]
    }
 ],
 "status" : "OK"
}      
我试过:
json.getJSONArray(“行”).getJSONObject(0).getJSONObject(“距离”).toString()


但是我总是得到org.json.JSONException:JSONObject[“distance”]未找到

使用
javajson.jar

将JSON字符串转换为object并获取元素

你可以这样做

试试这个:

package Sample;

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

public class TestMain 
{

    public static void main(String[] args) throws JSONException
    {
        String s="{  \"destination_addresses\" : [ \"New York City, New York, Verenigde Staten\" ],  \"origin_addresses\" : [ \"Washington D.C., District of Columbia, Verenigde Staten\" ],  \"rows\" : [    {       \"elements\" : [          {             \"distance\" : {                \"text\" : \"225 mijl\",                \"value\" : 361714             },             \"duration\" : {                \"text\" : \"3 uur 51 min.\",                \"value\" : 13877             },             \"status\" : \"OK\"          }       ]    } ], \"status\" : \"OK\"} ";

        JSONObject jsonObj = new JSONObject(s);

        JSONArray array= (JSONArray) jsonObj.get("rows");

        JSONObject jsonObj2 =(JSONObject) array.get(0);

        JSONArray array2= (JSONArray)jsonObj2.get("elements");

        JSONObject jsonObj3 =(JSONObject) array2.get(0);

        System.out.println(jsonObj3.get("distance"));

    }
}
json.getJSONArray("rows").getJSONObject(0).getJSONArray("elements").getJSONObject(0).getJSONObject("distance").toString();
输出:

{“文本”:“225 mijl”,“值”:361714}


使用
javajson.jar

将JSON字符串转换为object并获取元素

你可以这样做

试试这个:

package Sample;

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

public class TestMain 
{

    public static void main(String[] args) throws JSONException
    {
        String s="{  \"destination_addresses\" : [ \"New York City, New York, Verenigde Staten\" ],  \"origin_addresses\" : [ \"Washington D.C., District of Columbia, Verenigde Staten\" ],  \"rows\" : [    {       \"elements\" : [          {             \"distance\" : {                \"text\" : \"225 mijl\",                \"value\" : 361714             },             \"duration\" : {                \"text\" : \"3 uur 51 min.\",                \"value\" : 13877             },             \"status\" : \"OK\"          }       ]    } ], \"status\" : \"OK\"} ";

        JSONObject jsonObj = new JSONObject(s);

        JSONArray array= (JSONArray) jsonObj.get("rows");

        JSONObject jsonObj2 =(JSONObject) array.get(0);

        JSONArray array2= (JSONArray)jsonObj2.get("elements");

        JSONObject jsonObj3 =(JSONObject) array2.get(0);

        System.out.println(jsonObj3.get("distance"));

    }
}
json.getJSONArray("rows").getJSONObject(0).getJSONArray("elements").getJSONObject(0).getJSONObject("distance").toString();
输出:

{“文本”:“225 mijl”,“值”:361714}


我认为您缺少了第二个数组
“elements”
,您找到了数组“row”,因此获取对象
0
,在对象
0
中,您需要找到
“elements”
数组,然后再次获取对象
0
,这样您就可以获取
“distance”
对象

试试这个:

package Sample;

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

public class TestMain 
{

    public static void main(String[] args) throws JSONException
    {
        String s="{  \"destination_addresses\" : [ \"New York City, New York, Verenigde Staten\" ],  \"origin_addresses\" : [ \"Washington D.C., District of Columbia, Verenigde Staten\" ],  \"rows\" : [    {       \"elements\" : [          {             \"distance\" : {                \"text\" : \"225 mijl\",                \"value\" : 361714             },             \"duration\" : {                \"text\" : \"3 uur 51 min.\",                \"value\" : 13877             },             \"status\" : \"OK\"          }       ]    } ], \"status\" : \"OK\"} ";

        JSONObject jsonObj = new JSONObject(s);

        JSONArray array= (JSONArray) jsonObj.get("rows");

        JSONObject jsonObj2 =(JSONObject) array.get(0);

        JSONArray array2= (JSONArray)jsonObj2.get("elements");

        JSONObject jsonObj3 =(JSONObject) array2.get(0);

        System.out.println(jsonObj3.get("distance"));

    }
}
json.getJSONArray("rows").getJSONObject(0).getJSONArray("elements").getJSONObject(0).getJSONObject("distance").toString();

我想也是这样。我希望这对您有所帮助。

我认为您缺少了第二个数组
“elements”
,您找到了数组“row”,因此获取对象
0
,在对象
0
中,您需要找到
“elements”
数组,然后再次获取对象
0
,这样您就可以获取
“distance”
对象

试试这个:

package Sample;

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

public class TestMain 
{

    public static void main(String[] args) throws JSONException
    {
        String s="{  \"destination_addresses\" : [ \"New York City, New York, Verenigde Staten\" ],  \"origin_addresses\" : [ \"Washington D.C., District of Columbia, Verenigde Staten\" ],  \"rows\" : [    {       \"elements\" : [          {             \"distance\" : {                \"text\" : \"225 mijl\",                \"value\" : 361714             },             \"duration\" : {                \"text\" : \"3 uur 51 min.\",                \"value\" : 13877             },             \"status\" : \"OK\"          }       ]    } ], \"status\" : \"OK\"} ";

        JSONObject jsonObj = new JSONObject(s);

        JSONArray array= (JSONArray) jsonObj.get("rows");

        JSONObject jsonObj2 =(JSONObject) array.get(0);

        JSONArray array2= (JSONArray)jsonObj2.get("elements");

        JSONObject jsonObj3 =(JSONObject) array2.get(0);

        System.out.println(jsonObj3.get("distance"));

    }
}
json.getJSONArray("rows").getJSONObject(0).getJSONArray("elements").getJSONObject(0).getJSONObject("distance").toString();

我想也是这样。我希望这对您有所帮助。

您需要使用Jackson或Gson来反序列化json。您缺少一个
getJSONObject(“元素”)。getJSONObject(0)
之前。getJSONObject(“距离”)
您需要使用Jackson或Gson来反序列化json。您缺少一个
getJSONObject(“元素”)。getJSONObject(0)
之前的
.getJSONObject(“距离”)
它确实帮助了我!谢谢这当然对我有帮助!谢谢@PieterDeRop如果有助于解决您的问题,您可能希望接受答案。我只能接受一个答案,但我没有足够的信誉点来表示您的答案是有用的。@PieterDeRop如果有助于解决您的问题,您可能希望接受答案。我只能接受一个答案,但我还没有足够的信誉点来回答您的问题。@PieterDeRop说你的答案是有用的。