JSON Field name with double quote issue

782 views
Skip to first unread message

JB

unread,
Feb 25, 2009, 5:29:20 PM2/25/09
to google-gson
I am attempting to use gson to generate json to be sent back to the
client. The client is making an AJAX call and is using the
AutoComplete js lib.

When I send the JSON back to the client the json looks like this:

{ results: [
{"id":"55031","value":"Douglas","info":"016"}
{"id":"55027","value":"Dodge","info":"014"}
{"id":"55029","value":"Door","info":"015"}
{"id":"55025","value":"Dane","info":"013"}
{"id":"55033","value":"Dunn","info":"017"}
]}

The AutoComplete JS lib can't deal with the field name quotes. When I
change the server side to return a hard-coded value like this the
AutoComplete works.

{ results: [
{ id: "1", value: "Foobar", info: "Cheshire" },
{ id: "2", value: "Foobarfly", info: "Shropshire" },
{ id: "3", value: "Foobarnacle", info: "Essex" }
]}

Is it possible to gen the JSON without the double quotes on field
names? If so, does someone have an example?

Jacob Tomaw

unread,
Feb 26, 2009, 11:21:51 AM2/26/09
to googl...@googlegroups.com
JB,

This would not be valid json according to the spec http://json.org/.

The project has tried to only produce and consume valid json.

Jacob
--
Jacob Tomaw
tfl:The Flatiron Life (http://tomaw.com)
Follow me on Twitter! (http://twitter.com/JacobTomaw)

inder

unread,
Feb 26, 2009, 4:18:01 PM2/26/09
to google-gson
Gson supports consuming of field names without quotes or with single
quotes. We also allow the same input to mix it up with some field
names with double quotes, some with single quotes, or some with no
quotes at all.
However, we always produce valid JSON with double quotes around field
names. there is currently no option to change that behavior.
Can you file a bug on the other library to start accepting valid JSON
too, or is that not an option?

In your first example, I note that you are missing "," to separate out
the array entries. Could that be the cause of problem?

Inder



On Feb 26, 8:21 am, Jacob Tomaw <jacob.to...@gmail.com> wrote:
> JB,
>
> This would not be valid json according to the spechttp://json.org/.

JB

unread,
Feb 26, 2009, 4:21:18 PM2/26/09
to google-gson
Here that? That's me slamming my palm into my forehead.

I figured it out, my bad. The problem was not the quotes around the
field names it was at the root level. I forgot to double quote the
results: field. This is what is being sent back now and it works!

{ "results": [

{"id":"55017","value":"Chippewa","info":"009"},

{"id":"55015","value":"Calumet","info":"008"},

{"id":"55021","value":"Columbia","info":"011"},

{"id":"55019","value":"Clark","info":"010"},

{"id":"55023","value":"Crawford","info":"012"}]}


Thanks.
JB

Reply all
Reply to author
Forward
0 new messages