Java JSONObject.getJSONArray始终返回null
我想使用谷歌距离矩阵API获得在两个地点之间旅行所需的持续时间。但是当我试图从返回的数据(JSON编码)中获取持续时间时,getJSONArray方法总是返回null 以下是谷歌发送的数据:Java JSONObject.getJSONArray始终返回null,java,android,json,nullpointerexception,Java,Android,Json,Nullpointerexception,我想使用谷歌距离矩阵API获得在两个地点之间旅行所需的持续时间。但是当我试图从返回的数据(JSON编码)中获取持续时间时,getJSONArray方法总是返回null 以下是谷歌发送的数据: { "destination_addresses" : [ "Rome, Metropolitan City of Rome, Italy" ], "origin_addresses" : [ "Berlin, Germany" ], "rows" : [ {
{
"destination_addresses" : [ "Rome, Metropolitan City of Rome, Italy" ],
"origin_addresses" : [ "Berlin, Germany" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "1,501 km",
"value" : 1501458
},
"duration" : {
"text" : "15 hours 5 mins",
"value" : 54291
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
以下是获取持续时间的方法:
public static int getDurationFromJSON(String json){
try {
JSONObject jsonObj = new JSONObject(json)
.getJSONArray("rows")
.getJSONObject(0)
.getJSONArray ("elements")
.getJSONObject(0)
.getJSONObject("duration");
return (int)(jsonObj.getInt("value") / 60.0f + 0.5f);
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
getJSONArray(“行”)返回null。我不确定为什么会得到null,但这行似乎太多了:
(int)(Integer.parseInt(String.valueOf(jsonObj.getInt("value"))) / 60.0f + 0.5f);
getInt(“Value”)将返回一个int,为什么要将其转换为字符串,然后将其解析回int,然后再将其转换回int
这可以简单地简化为这样
return(int)((jsonObj.getInt("value")/60.0f) +0.5f)
至于空值,我将使用调试器检查传入的JSON,并确保它是您所认为的
另外,正如其他人所建议的,使用restTemplate之类的工具将json自动解析为本地映射对象将使您的生活更加轻松。好的,这里是解决方案。不要信任org.json。*使用Gson: Json数据:
{
"destination_addresses" : [ "Rome, Metropolitan City of Rome, Italy" ],
"origin_addresses" : [ "Berlin, Germany" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "1,501 km",
"value" : 1501458
},
"duration" : {
"text" : "15 hours 5 mins",
"value" : 54291
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
为结果创建对象:
public class DirectionMatrixResult {
private String[] destination_addresses;
private String[] origin_addresses;
private DirectionMatrixResultRow[] rows;
public DirectionMatrixResultRow[] getRows() {
return rows;
}
public String[] getDestination_addresses() {
return destination_addresses;
}
public String[] getOrigin_addresses() {
return origin_addresses;
}
public void setDestination_addresses(String[] destination_addresses) {
this.destination_addresses = destination_addresses;
}
public void setOrigin_addresses(String[] origin_addresses) {
this.origin_addresses = origin_addresses;
}
public void setRows(DirectionMatrixResultRow[] rows) {
this.rows = rows;
}
}
public class DirectionMatrixResultRow {
private DirectionMatrixResultElement[] elements;
public DirectionMatrixResultElement[] getElements() {
return elements;
}
public void setElements(DirectionMatrixResultElement[] elements) {
this.elements = elements;
}
}
public class DirectionMatrixResultElement {
private DirectionMatrixResultElementValue distance;
private DirectionMatrixResultElementValue duration;
private String status;
public DirectionMatrixResultElementValue getDistance() {
return distance;
}
public DirectionMatrixResultElementValue getDuration() {
return duration;
}
public String getStatus() {
return status;
}
public void setDistance(DirectionMatrixResultElementValue distance) {
this.distance = distance;
}
public void setDuration(DirectionMatrixResultElementValue duration) {
this.duration = duration;
}
public void setStatus(String status) {
this.status = status;
}
}
public class DirectionMatrixResultElementValue {
private String text;
private long value;
public long getValue() {
return value;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public void setValue(long value) {
this.value = value;
}
}
然后打电话:
public static int getDurationFromJSON(String json){
try {
Gson gson = new Gson();
DirectionMatrixResult result = gson.fromJson(json, DirectionMatrixResult.class);
return (int)(result.getRows()[0].getElements()[0].getDuration().getValue() / 60.0f + 0.0f);
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
您是否检查从Google API收到的数据。json变量的内容是什么?json变量的数据是发布的json代码。它与我在浏览器中启动请求时得到的数据完全相同。但是您确定您将相同的内容输入变量“json”中吗“调试代码时?我100%确定。我建议您使用调试器查看jsonObj变量中的内容。如果函数返回null,则意味着没有任何内容,或者它不是预期的类型,如果您说您对内容有把握,则您请求的类型错误。实际上,您提到的行是我以字符串形式获取持续时间的版本的遗留内容。现在它没有任何意义。