不是 JSON 对象异常

Not a JSON Object Exception

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

这是我发送和接收请求和响应的 class。

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() 的字符串输出。所以我猜信息检索是有效的,即使它在代码的错误部分。

Plus 在代码的同一个 try-catch-block (Distance24Client, method "toDistance24") 中说,错误发生在第 30 行,即 return 语句 "toDistance24"方法。

(可点击)

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

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

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

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

错了。将距离读取为 JsonArray。

创建处理数组或非数组的方法

public static JsonElement toJsonElement(String jsonString) {
    JsonParser parser = new JsonParser();
    JsonElement jsonElement = parser.parse(jsonString);

    JsonElement result = null;
    if (jsonElement instanceof JsonObject) {
       result = jsonElement.getAsJsonObject();
    } else if (jsonElement instanceof JsonArray) {
       result =  jsonElement.getAsJsonArray();
    } else {
       throw new IllegalArgumentException(jsonString + " is not valid JSON stirng");
    }

    return result;
}