Missing Double Quotes

116 views
Skip to first unread message

Alexandre Vieira

unread,
Apr 28, 2013, 2:41:23 PM4/28/13
to googl...@googlegroups.com
Hello,

My app fails to parse a JSON string because it expects double quotes around the fields, I think.


Context:

The receives JSON responses from a server. A response always has three fields and the third can be anything (including arrays), depending on the type of request. For example,

{
   "status":"OK",
   "reason":"",
   "data":null
}

or

{
   "status":"OK",
   "reason":"",
   "data":{
      "id":34,
      "name":"name19",
      "email":"email19",
      "registerType":null,
      "password":null
   }
}

Naturally, I created a superclass that declares the three fields common to every response. Then, each subclass handles the field 'data' as appropriate (it simply uses a shared Gson object to parse that field into a POJO).

public class Response
{
//...
@Key private String status;
@Key private String reason;
@Key private Object data;
//...
}


Problem:

Now, the response is correctly parsed into a subclass of 'Response', meaning the field 'data' is processed and stored in 'Response.data'. However, when I attempt to deserialize its content, the parser fails and complains about a malformed JSON. When printed, 'Response.data' contains no quotes, as did the original JSON.

I'm not all too familiar with GSON and JSON in general, so I would greatly appreciate any piece of advice or solution the problem I'm facing.

Thank you,

Alexandre Vieira

Alexandre Vieira

unread,
Apr 29, 2013, 11:59:21 AM4/29/13
to googl...@googlegroups.com
Resolved.

I decided to make the field 'data' a generic type and now every response is correctly deserialized as expected.

ashish jain

unread,
May 29, 2013, 7:36:58 AM5/29/13
to googl...@googlegroups.com
I'm also facing the same problem. Is there any other way to solve this other than making field 'data' as generic type??

Inderjeet Singh

unread,
May 29, 2013, 2:13:16 PM5/29/13
to googl...@googlegroups.com
You can write a custom type adapter for Response.
Reply all
Reply to author
Forward
0 new messages