添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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);
                Thank you! My HTTP Class downloaded the json string in a 256 byte char buffer which was then converted to a string (result1). Because of that, result1 had a length of 255 instead of 169.
– Non
                Jul 15, 2012 at 20:13
                I had the same problem; the whole EOF thing in the exception kind of threw me, my problem was just pure and simple badly formed JSON.
– Kong
                Sep 22, 2013 at 6:26
                I had the same problem, but I obtained my JSON via a FileChannel. I was pretty sure encoding was allright. What I did was to call trim() method on the String red from file on HDD, this solved the problem.
– user1930502
                Apr 14, 2014 at 18:20
                @ThePaul - you probably had extra NUL (that is, '\0') characters at the end of your string. String.trim() removes not just spaces, but also any character less than 32, whereas GSON counts those characters as trailing garbage. (And it then complains "I saw the closing brace, so should next see an EOF, but I saw some other stuff instead ")
– Daniel Martin
                Apr 15, 2014 at 11:33
                Make sure you are importing the right json reader, other libraries have classes of the same name
– Joe Maher
                Dec 23, 2015 at 1:04

From my recent experience, JsonReader#setLenient basically makes the parser very tolerant, even to allow malformed JSON data.

But for certain data retrieved from your trusted RESTful API(s), this error might be caused by trailing white spaces. In such cases, simply trim the data would avoid the error:

String trimmed = result1.trim();

Then gson.fromJson(trimmed, T) might work. Surely this only covers a special case, so YMMV.

What if white spaces mattered in the string response? Tirmming is not good in that case. You'll be losing the integrity of your data. – Anis LOUNIS aka AnixPasBesoin Feb 3, 2017 at 16:10

In the debugger you don't need to add back slashes, the input field understands the special chars.

In java code you need to escape the special chars

I copied the value of result1 from the debugger and added backslashes afterwards to escape the special chars in result2 ;) – Non Jul 14, 2012 at 14:07 Could it be there is a space char or \n\r infront or appended to the json string. It should be something simple – Mark Bakker Jul 14, 2012 at 16:28 @ProgrammerBruce: result1.equals(result2)=false. Both strings contained the same characters, but result1 was longer then result2 (caused by my http class; see above). – Non Jul 15, 2012 at 20:28