0

How can I get the value of "distance" out of the following JSON object with java?

{
  "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"
}      

I tried: json.getJSONArray("rows").getJSONObject(0).getJSONObject("distance").toString();

But I always get org.json.JSONException: JSONObject["distance"] not found.

2
  • You need to use Jackson or Gson in order to deserialise a json. Commented Jun 10, 2016 at 19:33
  • You are missing a getJSONObject("elements").getJSONObject(0) before .getJSONObject("distance") Commented Jun 10, 2016 at 21:17

2 Answers 2

2

I think you are missing the second array "elements", you find array "row" so get object 0, in object 0 you need to find the "elements" array then take the object 0 again, so you can get the "distance" object.

Try this:

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

Again, I think. I hope this helped you.

Sign up to request clarification or add additional context in comments.

1 Comment

It certainly helped me ! Thanks !
0

Use java-json.jar

Convert JSON String to object and fetch the element.

You can refer this.

Try this:

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"));

    }
}

OUTPUT:

{"text":"225 mijl","value":361714}

2 Comments

@PieterDeRop You might want to accept the answer if it helped to solve your question.
I can only accept one answer and I don't have enough reputation point yet to say your answer is usefull.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.