Error parsing JSON

36 views
Skip to first unread message

Luke

unread,
Aug 18, 2009, 1:03:23 AM8/18/09
to Google Web Toolkit

Below is json object that I am having trouble with. When I try to use
this object with JSONParser.parse(), I get JSONException. I have
verified that the JSON object is of valid form but cannot figure out
why I am getting the exception. I have narrowed it down to the
particular line of code that contains JSONParser.parse();

Anyone see anything here?

{"projects":[{"id":"1","title":"Project Number
1","priority":"High","openDate":"2009-06-05","closeDate":"2009-06-07","owner":
{"firstName":"John","lastName":"Doe","email":"m...@email.com","site":"http://
www.mysite.com"},"notes":[{"title":"This is a note","content":"This is
a note. I am typing this to be long just so that it will show up as
scrollable when necessary.
","timestamp":"2009-06-07 13:28:55"},{"title":"Last note. Will be two
there.","content":"This is a note. I am typing this to be long just
so that it will show up as scrollable when necessary.
","timestamp":"2009-06-07 13:30:12"}],"pictures":[]},
{"id":"2","title":"Project Number
2","priority":"Medium","openDate":"2009-05-20","closeDate":"2009-06-06","owner":
{"firstName":"John","lastName":"Doe","email":"m...@email.com","site":"http://
www.mysite.com"},"notes":[{"title":"Another Note","content":"This is a
note. I am typing this to be long just so that it will show up as
scrollable when necessary.
","timestamp":"2009-06-07 13:29:09"}],"pictures":[]},
{"id":"3","title":"Project Number
3","priority":"High","openDate":"2009-06-04","closeDate":"","owner":
{"firstName":"Tom","lastName":"Smith","email":"t...@email.com","site":"http://
www.tomsite.com"},"notes":[{"title":"Note again","content":"This is a
note. I am typing this to be long just so that it will show up as
scrollable when necessary.
","timestamp":"2009-06-07 13:29:25"}],"pictures":[]}]}

Jeff Chimene

unread,
Aug 18, 2009, 1:43:40 PM8/18/09
to google-we...@googlegroups.com
On 08/17/2009 10:03 PM, Luke wrote:
>
>
> Below is json object that I am having trouble with. When I try to use
> this object with JSONParser.parse(), I get JSONException. I have
> verified that the JSON object is of valid form but cannot figure out
> why I am getting the exception. I have narrowed it down to the
> particular line of code that contains JSONParser.parse();
>
> Anyone see anything here?

Well, all that JSONParser() does is to call eval() on the string. Try
wrapping the object in parenthesis:

$! Notice that the string is now quoted.
$! This simulates what that client receives.
$ head /tmp/foo.js
x ='{"projects":[{"id":"1","title":"Project Number


1","priority":"High","openDate":"2009-06-05","closeDate":"2009-06-07","owner

<snip>

Well I'm a Rhino man
Well, don't you know I am?
(apologies to F. Zappa)

$ rhino
js> load("/tmp/foo.js")
js> print(x.toSource())
(new String("{\"projects\":[{\"id\":\"1\",\"title\":\"Project Number


1\",\"priority\":\"High\",\"openDate\":\"2009-06-05\",\"closeDate\":\"2009-06-07\",\"owner\":

{\"firstName\":\"John\",\"lastName\":\"Doe\
<snip>

js> print (eval(x))
js: "<stdin>#4(eval)", line 1: uncaught JavaScript runtime exception:
SyntaxError: missing ; before statement
at <stdin>:4
<snip>

js> print(eval('('+x+')'))
[object Object]

Thomas Broyer

unread,
Aug 18, 2009, 7:01:05 PM8/18/09
to Google Web Toolkit


On 18 août, 19:43, Jeff Chimene <jchim...@gmail.com> wrote:
> On 08/17/2009 10:03 PM, Luke wrote:
>
>
>
> > Below is json object that I am having trouble with.  When I try to use
> > this object with JSONParser.parse(), I get JSONException.  I have
> > verified that the JSON object is of valid form but cannot figure out
> > why I am getting the exception.  I have narrowed it down to the
> > particular line of code that contains JSONParser.parse();
>
> > Anyone see anything here?
>
> Well, all that JSONParser() does is to call eval() on the string. Try
> wrapping the object in parenthesis:

Well, JSONParser actually calls eval() *with* the wrapping
parentheses.

Jeff Chimene

unread,
Aug 18, 2009, 7:12:12 PM8/18/09
to google-we...@googlegroups.com

Are they added in the parent?

Checking the trunk source, I don't see that concatenation:

public static JSONValue parse(String jsonString) {
if (jsonString == null) {
throw new NullPointerException();
}
if (jsonString.length() == 0) {
throw new IllegalArgumentException("empty argument");
}
try {
return evaluate(jsonString);
} catch (JavaScriptException ex) {
throw new JSONException(ex);
}
}

See
http://code.google.com/p/google-web-toolkit/source/browse/trunk/user/src/com/google/gwt/json/client/JSONParser.java

Thomas Broyer

unread,
Aug 19, 2009, 3:56:01 AM8/19/09
to Google Web Toolkit
From the very same file:
private static native JSONValue evaluate(String jsonString) /*-{
var v = eval('(' + jsonString + ')');
var func = @com.google.gwt.json.client.JSONParser::typeMap[typeof
v];
return func ? func(v) :
@com.google.gwt.json.client.JSONParser::throwUnknownTypeException
(Ljava/lang/String;)(typeof v);
}-*/;

Jeff Chimene

unread,
Aug 19, 2009, 12:00:52 PM8/19/09
to google-we...@googlegroups.com

Well, yes. But the OP said he's using JSONParser.parse() How does
JSONValue.evaluate() get called in that scenario?

Jeff Chimene

unread,
Aug 19, 2009, 12:02:48 PM8/19/09
to google-we...@googlegroups.com

Oh, never mind. I figured it out.

Luke Howell

unread,
Aug 19, 2009, 12:06:35 PM8/19/09
to google-we...@googlegroups.com
I actually figured out the problem. There were '\n' characters in the
string I was trying to parse. Once removed that solved the problem.

Sent via iPhone
Reply all
Reply to author
Forward
0 new messages