gson抛出MalformedJsonException [英] gson throws MalformedJsonException

查看:578
本文介绍了gson抛出MalformedJsonException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 gson json 字符串转换为Java对象。
result2 的值与 result1 的值完全相同。 (从调试器复制;添加了反斜杠)

转换result1时抛出以下异常:
com.google.gson.JsonSyntaxException:com.google .gson.stream.MalformedJsonException:预计在第1行的EOF 170



转换 result2 正常工作。



json字符串根据jsonlint.com有效。

  public static Userinfo getUserinfo( )
{
String result1 = http.POST(https://www.bitstamp.net/api/balance/,
postdata,true);
String result2 ={\btc_reserved \:\0 \,\fee \:\0.5000 \,\btc_available \:\\ 0.10000000 \,\usd_reserved\:\0 \,\btc_balance \:\0.10000000 \,\usd_balance \:\ 30.00 \,\usd_available \:\30.00\};
Gson gson = new Gson();
Userinfo userinfo1 = gson.fromJson(result1,Userinfo.class); //引发异常
Userinfo userinfo2 = gson.fromJson(result2,Userinfo.class); //正常工作

返回userinfo1;
}
private class Userinfo {

public Userinfo(){
}

public float usd_balance;
public float btc_balance;
public float usd_reserved;
public float btc_reserved;
public float usd_available;
public float btc_available;
公众流动资金费用;
public float last_update;
}


解决方案

我怀疑result1有一些在结尾处的字符,您在调试器中看不到关闭的} 字符。 result1 result2 的长度是多少?我会注意到 result2 ,因为您引用了169个字符。



在对象结束后还有一些不是空格的字符,它非常狭窄地定义了空格(正如JSON规范所做的那样) - 只有 \ t \\\
\r ,空格计为空格。 特别要注意的是,尾随的NUL( \ 0 )字符不会被视为空格,并会导致此错误


$ b

如果您无法轻易找出最终导致多余字符的原因并消除它们,另一个选择是告诉GSON以宽松模式解析:

  Gson gson = new Gson(); 
JsonReader reader = new JsonReader(new StringReader(result1));
reader.setLenient(true);
Userinfo userinfo1 = gson.fromJson(reader,Userinfo.class);


I'm using gson to convert a json string to a Java-Object. The value of result2 is exactly the same as the value of result1. (Copied from debugger; Backslashs added)

The following exception is thrown while converting result1: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected EOF at line 1 column 170

Converting result2 works fine.

The json string is valid according to jsonlint.com.

public static Userinfo getUserinfo()
{
    String result1 = http.POST("https://www.bitstamp.net/api/balance/",
                                postdata, true);
    String result2 = "{\"btc_reserved\": \"0\", \"fee\": \"0.5000\", \"btc_available\": \"0.10000000\", \"usd_reserved\": \"0\", \"btc_balance\": \"0.10000000\", \"usd_balance\": \"30.00\", \"usd_available\": \"30.00\"}";
    Gson gson = new Gson();
    Userinfo userinfo1 = gson.fromJson(result1, Userinfo.class); //throws Exception
    Userinfo userinfo2 = gson.fromJson(result2, Userinfo.class); //works fine

    return userinfo1;
}
private class Userinfo {

    public Userinfo(){
    }

    public float usd_balance;
    public float btc_balance ;
    public float usd_reserved;
    public float btc_reserved;
    public float usd_available;
    public float btc_available;
    public float fee;
    public float last_update;
}

解决方案

I suspect that result1 has some characters at the end of it that you can't see in the debugger that follow the closing } character. What's the length of result1 versus result2? I'll note that result2 as you've quoted it has 169 characters.

GSON throws that particular error when there's extra characters after the end of the object that aren't whitespace, and it defines whitespace very narrowly (as the JSON spec does) - only \t, \n, \r, and space count as whitespace. In particular, note that trailing NUL (\0) characters do not count as whitespace and will cause this error.

If you can't easily figure out what's causing the extra characters at the end and eliminate them, another option is to tell GSON to parse in lenient mode:

Gson gson = new Gson();
JsonReader reader = new JsonReader(new StringReader(result1));
reader.setLenient(true);
Userinfo userinfo1 = gson.fromJson(reader, Userinfo.class);

这篇关于gson抛出MalformedJsonException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆