0

Good Day I have question about JSON parsing . How to get the value of distance with this json format

{
   "destination_addresses" : [
      "Davao City-Juction Digos Section Road, Talomo, Davao City, Davao del Sur, Philippines"
   ],
   "origin_addresses" : [
      "Roxas Ave, Poblacion District, Davao City, Davao del Sur, Philippines"
   ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "10.8 km",
                  "value" : 10782
               },
               "duration" : {
                  "text" : "55 mins",
                  "value" : 3309
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

These are my code:

 JSONObject jObj = new JSONObject(result);
                JSONObject a = jObj.getJSONObject("rows");
                JSONObject b = a.getJSONObject("elements");
                JSONObject c= b.getJSONObject("distance");
                String est = c.getString("text");
                String vals = c.getString("value");

These are the error :

aorouteguide.example.com.davaorouteguideapp E/MYAPP: unexpected JSON exception
                                                                                       org.json.JSONException: Value [{"elements":[{"distance":{"text":"31.4 km","value":31351},"duration":{"text":"1 hour 26 mins","value":5136},"status":"OK"}]}] at rows of type org.json.JSONArray cannot be converted to JSONObject
jclitang
  • 5
  • 3

2 Answers2

2

"rows" is a json array with one "row" object in it. That row object consists of an array of "elements", which has one "element" in it. You must use getJSONArray instead of getJSONObject to retrieve them. See code:

    JSONObject jObj = new JSONObject(result);
    JSONArray rows = jObj.getJSONArray("rows");
    JSONObject row = rows.getJSONObject(0);
    JSONArray elementsArray = row.getJSONArray("elements");
    JSONObject element = elementsArray.getJSONObject(0);
    JSONObject distance = element.getJSONObject("distance");
    String est = distance.getString("text");
    String vals = distance.getString("value");
ahomphophone
  • 319
  • 1
  • 8
0

You can create object from json string :

-----------------------------------com.example.Distance.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Distance {

@SerializedName("text")
@Expose
private String text;
@SerializedName("value")
@Expose
private Integer value;

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public Integer getValue() {
return value;
}

public void setValue(Integer value) {
this.value = value;
}

}

-----------------------------------com.example.Duration.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Duration {

@SerializedName("text")
@Expose
private String text;
@SerializedName("value")
@Expose
private Integer value;

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public Integer getValue() {
return value;
}

public void setValue(Integer value) {
this.value = value;
}

}

-----------------------------------com.example.Element.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Element {

@SerializedName("distance")
@Expose
private Distance distance;
@SerializedName("duration")
@Expose
private Duration duration;
@SerializedName("status")
@Expose
private String status;

public Distance getDistance() {
return distance;
}

public void setDistance(Distance distance) {
this.distance = distance;
}

public Duration getDuration() {
return duration;
}

public void setDuration(Duration duration) {
this.duration = duration;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

}

-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("destination_addresses")
@Expose
private List<String> destinationAddresses = null;
@SerializedName("origin_addresses")
@Expose
private List<String> originAddresses = null;
@SerializedName("rows")
@Expose
private List<Row> rows = null;
@SerializedName("status")
@Expose
private String status;

public List<String> getDestinationAddresses() {
return destinationAddresses;
}

public void setDestinationAddresses(List<String> destinationAddresses) {
this.destinationAddresses = destinationAddresses;
}

public List<String> getOriginAddresses() {
return originAddresses;
}

public void setOriginAddresses(List<String> originAddresses) {
this.originAddresses = originAddresses;
}

public List<Row> getRows() {
return rows;
}

public void setRows(List<Row> rows) {
this.rows = rows;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

}

-----------------------------------com.example.Row.java-----------------------------------

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Row {

@SerializedName("elements")
@Expose
private List<Element> elements = null;

public List<Element> getElements() {
return elements;
}

public void setElements(List<Element> elements) {
this.elements = elements;
}

}

And :

Gson gson = new Gson();
Example ex= gson.fromJson(**yourJson**.toString(), Example .class);

you can get Distance class from ex easily! I hope it can help your problem!

Thientvse
  • 1,753
  • 1
  • 14
  • 23