在JSON解析android [重复]中获取价值

问题描述 投票:0回答:2

这个问题在这里已有答案:

美好的一天我对JSON解析有疑问。如何使用此json格式获取距离值

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

这些是我的代码:

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

这些是错误:

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
android json
2个回答
2
投票

“rows”是一个json数组,其中包含一个“row”对象。该行对象由一系列“元素”组成,其中包含一个“元素”。您必须使用getJSONArray而不是getJSONObject来检索它们。看代码:

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

0
投票

您可以从json字符串创建对象:

-----------------------------------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;
}

}

而且:

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

你可以从前轻松获得远程课程!我希望它可以帮助你解决问题!

© www.soinside.com 2019 - 2024. All rights reserved.