试图在Java中解析JSON(使用Gson),得到malformmedjsonexception



我正在尝试从WeatherUnderground API获取天气预报数据。

到目前为止,我使用以下代码:
URLConnection connection = url.openConnection();
connection.setConnectTimeout(5000);
connection.connect();
JsonParser jp = new JsonParser();
JsonElement forecastJson = jp.parse(new InputStreamReader((InputStream) connection.getContent())).getAsJsonObject()
        .getAsJsonObject().get("forecast")
        .getAsJsonObject().get("simpleforecast")
        .getAsJsonObject().getAsJsonArray("forecastday").get(1);
System.out.println("forecastJson = " + forecastJson.toString());
String date = String.valueOf(jp.parse(forecastJson
        .getAsJsonObject().get("date")
        .getAsJsonObject().get("epoch").getAsString()));
String high = String.valueOf(jp.parse(forecastJson
        .getAsJsonObject().get("high")
        .getAsJsonObject().get("celsius").getAsString()));
String low = String.valueOf(jp.parse(forecastJson
        .getAsJsonObject().get("low")
        .getAsJsonObject().get("celsius").getAsString()));
String conditions;
try {
    conditions = String.valueOf(jp.parse(forecastJson
            .getAsJsonObject().get("conditions").getAsString()));
} catch (JsonSyntaxException e) {
    e.printStackTrace();
}

我收到的JsonElement forecastJson"是这样的:

{
  "date": {
    "epoch": "1467046800",
    "pretty": "7:00 PM CEST on June 27, 2016",
    "day": 27,
    "month": 6,
    "year": 2016,
    "yday": 178,
    "hour": 19,
    "min": "00",
    "sec": 0,
    "isdst": "1",
    "monthname": "June",
    "monthname_short": "Jun",
    "weekday_short": "Mon",
    "weekday": "Monday",
    "ampm": "PM",
    "tz_short": "CEST",
    "tz_long": "Europe/Berlin"
  },
  "period": 2,
  "high": {
    "fahrenheit": "77",
    "celsius": "25"
  },
  "low": {
    "fahrenheit": "58",
    "celsius": "14"
  },
  "conditions": "Partly Cloudy",
  "icon": "partlycloudy",
  "icon_url": "http://icons.wxug.com/i/c/k/partlycloudy.gif",
  "skyicon": "",
  "pop": 0,
  "qpf_allday": {
    "in": 0,
    "mm": 0
  },
  "qpf_day": {
    "in": 0,
    "mm": 0
  },
  "qpf_night": {
    "in": 0,
    "mm": 0
  },
  "snow_allday": {
    "in": 0,
    "cm": 0
  },
  "snow_day": {
    "in": 0,
    "cm": 0
  },
  "snow_night": {
    "in": 0,
    "cm": 0
  },
  "maxwind": {
    "mph": 15,
    "kph": 24,
    "dir": "W",
    "degrees": 260
  },
  "avewind": {
    "mph": 11,
    "kph": 18,
    "dir": "W",
    "degrees": 260
  },
  "avehumidity": 48,
  "maxhumidity": 0,
  "minhumidity": 0
}

我可以得到"日期","高"one_answers"低"字符串,但我无法得到"条件",我不明白我做错了什么。

我得到以下异常:

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON

据我所知,JSON不是畸形的。我应该如何得到"条件"的值?

我也尝试过其他JSON解析器/库,但都没有成功。我想继续与Gson-Library合作,我想我已经很接近了,但是卡住了。

谢谢你的帮助

如果你分解抛出异常的行,你可以看到你实际上是试图解析为JSON的字符串,实际上是而不是 JSON。

try {
    JsonObject jo = forecastJson.getAsJsonObject();
    JsonElement je = jo.get("conditions");
    String s1 = je.getAsString();
    // at this point s1 contains the value "Partly Cloudy" which you 
    // are trying to parse as JSON.
    JsonElement je2  = jp.parse(s1);

    conditions = String.valueOf(je2);
} catch (JsonSyntaxException e) {
    e.printStackTrace();
}

正如"JB Nizet"在我的问题下的评论中指出的那样,每当我想要获得另一部分内容时,我都试图再次解析JSON。那是我(刚开始)犯的错误。

现在我只解析一次,然后直接获取内容,没有任何问题:

JsonParser jp = new JsonParser();
JsonElement forecastJson = jp.parse(new InputStreamReader((InputStream) connection.getContent())).getAsJsonObject()
        .getAsJsonObject().get("forecast")
        .getAsJsonObject().get("simpleforecast")
        .getAsJsonObject().getAsJsonArray("forecastday").get(1);
String date = forecastJson.getAsJsonObject().get("date")
        .getAsJsonObject().get("epoch").getAsString();
String high = forecastJson.getAsJsonObject().get("high")
        .getAsJsonObject().get("celsius").getAsString();
String low = forecastJson.getAsJsonObject().get("low")
        .getAsJsonObject().get("celsius").getAsString();
String conditions = forecastJson.getAsJsonObject()
        .get("conditions").getAsString();

首先,正如他们在评论中所说的,真的没有必要再解析每一段。

String date = String.valueOf(jp.parse(forecastJson .getAsJsonObject().get("date") .getAsJsonObject().get("epoch").getAsString())); 与: String date = forecastJson.getAsJsonObject().get("date") .getAsJsonObject().get("epoch").getAsString();

你正在经历的问题实际上是我怀疑是Gson库中的错误。当它尝试解析带有空格的字符串时,它将对该字符串进行标记,并期望在第一个单词(在本例中是部分)之后文档应该结束,并且json格式错误,因为它没有到达文档的末尾。

所以要解决这个问题,要么按照注释说的做,这对你来说是合理的。或者如果你不愿意,你可以把部分多云改成例如Partly_cloudy这样就可以了:)

相关内容

  • 没有找到相关文章

最新更新