Trying to parse the following JSON return to java usable code:
{
"destination_addresses" : [ "New York, NY, USA" ],
"origin_addresses" : [ "Washington, DC, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "225 mi",
"value" : 361951
},
"duration" : {
"text" : "3 hours 51 mins",
"value" : 13876
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
// Parse the whole JSON result.
JSONObject o1 =(JSONObject)JSONValue.parse(outputString);
JSONArray a1 = (JSONArray) o1.get("rows");//2
for (int i = 0; i < a1.size(); i++) {
JSONObject o2 = (JSONObject) a1.get(i);//3
JSONArray a2 = (JSONArray) o2.get("elements");//4
for (int j = 0; j < a2.size(); j++) {
JSONObject o3 = (JSONObject) a2.get(j);//3
JSONArray a3 = (JSONArray) o3.get("distance");//4
for(int k =0; k < a3.size(); k++) {
System.out.println(((JSONObject) a3.get(k)).get(
"text").toString());//5
}
}
}
I would like to access text value of distance and also text value of duration . I found code to access distance and duration but when I added the third for loop following the example of the old ones I get this expection
Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray
At this line:
JSONArray a3 = (JSONArray) o3.get("distance");//4
Any help or suggestions on how do it in other ways? Thanks