gpt4 book ai didi

java - 不是 JSON 对象异常

转载 作者:行者123 更新时间:2023-11-30 08:07:06 27 4
gpt4 key购买 nike

我正在尝试从 Distance24 获取 JSON 值通过 Google GSON 输出 JSON。但我无法弄清楚异常的来源和来源(我正在使用带有 Java 的 Google AppEngine)。

这是我发送和获取请求和响应的类。

package de.tum.in.eist.distance;

import java.io.IOException;

import javax.inject.Inject;
import java.net.URL;

import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.gson.JsonObject;

import de.tum.in.eist.JsonHelper;
import de.tum.in.eist.URLFetchServiceHelper;

public class Distance24Client {

private final URLFetchService service;

@Inject
public Distance24Client(URLFetchService service) {
this.service = service;
}

public Distance24 getDistanceAPI(String source, String destination) throws IOException {
URL url = new URL("http://www.distance24.org/route.json?stops=" + source + "|" + destination);
HTTPResponse response = service.fetch(url);
String jsonString = URLFetchServiceHelper.toString(response);
try {
JsonObject json = JsonHelper.parse(jsonString);
return toDistance24(json);
} catch (Exception e) {
throw new IOException("Error ocurred in getDistanceAPI(): " + e.getMessage());
}
}

private Distance24 toDistance24(JsonObject response) {
if (!(response.get("stops").getAsJsonObject().getAsJsonArray().size() != 0)) {
throw new IllegalArgumentException("No Status set from Distance24 API");
} else {
JsonObject distances = response.get("distances").getAsJsonObject();
return new Distance24(distances);
}
}

}

这是 Distance24 对象:

package de.tum.in.eist.distance;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class Distance24 {

private int[] distances;
private int totalDistance;
private Double sourceLat;
private Double sourceLon;
private Double destLat;
private Double destLong;

public Distance24(JsonObject distances) {
this.setDistances(getIntArray(distances));
this.setTotalDistance(getSum(this.distances));
this.setSourceLat(distances.get("stops").getAsJsonObject().getAsJsonArray().get(0).getAsJsonObject().get("latitude").getAsDouble());
this.setSourceLon(distances.get("stops").getAsJsonObject().getAsJsonArray().get(0).getAsJsonObject().get("longitude").getAsDouble());
this.setDestLat(distances.get("stops").getAsJsonObject().getAsJsonArray().get(1).getAsJsonObject().get("latitude").getAsDouble());
this.setDestLong(distances.get("stops").getAsJsonObject().getAsJsonArray().get(1).getAsJsonObject().get("longitude").getAsDouble());
}

private int[] getIntArray(JsonObject array) {
JsonArray distances = array.getAsJsonArray();
int[] result = new int[distances.size()];
for(int i = 0; i < distances.size(); i++) {
result[i] = distances.get(i).getAsInt();
}

return result;
}

private int getSum(int[] array) {
int sum = 0;
for(int element : array) {
sum += element;
}

return sum;
}

private void setDistances(int[] distances) {
this.distances = distances;
}

public int getTotalDistance() {
return totalDistance;
}

public void setTotalDistance(int totalDistance) {
this.totalDistance = totalDistance;
}

public Double getSourceLat() {
return sourceLat;
}

public void setSourceLat(Double sourceLat) {
this.sourceLat = sourceLat;
}

public Double getSourceLon() {
return sourceLon;
}

public void setSourceLon(Double sourceLon) {
this.sourceLon = sourceLon;
}

public Double getDestLat() {
return destLat;
}

public void setDestLat(Double destLat) {
this.destLat = destLat;
}

public Double getDestLong() {
return destLong;
}

public void setDestLong(Double destLong) {
this.destLong = destLong;
}

}

结果,我得到了整个 JSON 对象作为 e.getMessage() 的字符串输出。所以我猜信息检索是有效的,即使它位于代码的错误部分。

加上在代码的同一个try-catch block 中(Distance24Client,方法“toDistance24”),它说,错误发生在第30行,这是“toDistance24”方法的返回语句。

Output

(可点击)

最佳答案

正在运行http://www.distance24.org/route.json?stops=detroit|dublin从我的浏览器给我

{"stops":[{"region":"Michigan ...
"distances":[5581]}

所以 distances 是一个数组而不是一个对象。所以你的路线:

JsonObject distances = response.get("distances").getAsJsonObject();

是错误的。将距离读取为 JsonArray。

关于java - 不是 JSON 对象异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30940626/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com