aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes JSON Syntax error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "JSON Syntax error" Watch "JSON Syntax error" New topic
Author

JSON Syntax error

Johnny Chan
Greenhorn

Joined: Mar 05, 2009
Posts: 14
I'm using java servlet to send back JSON data to client side javascript. I build a JSONArray of string values using json-lib and it is received and is even parsed by my javascript, but in the firefox error console i get a syntax error:

["A","B","C"]

and the arrow points to between the " and the A. Any idea what is causing this?

thanks-jc
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Do you not think that showing us the parsing code might be helpful?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Johnny Chan
Greenhorn

Joined: Mar 05, 2009
Posts: 14
the main page calls getCBParameter,


Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Is ["A","B","C"] the entire thing that is being returned from the server? If so that is not valid JSON syntax. It should look like


Eric
Johnny Chan
Greenhorn

Joined: Mar 05, 2009
Posts: 14
Yes, its the only thing being returned by the server.
Johnny Chan
Greenhorn

Joined: Mar 05, 2009
Posts: 14
I've tried to do that, but then i get something like:



and it still gives a syntax error because i think "values" is in quotes. So perhaps it is how i am creating the jsonobject using json-lib that is incorrect.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
That does not look anything like I posted.

What is generating the serverside JSON?

Eric

Johnny Chan
Greenhorn

Joined: Mar 05, 2009
Posts: 14


So the response here is:


and the syntax error is pointing to the {" in characters 2 - 3.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
The output should look like



So you either need to do


or

You can use the JSONObject [guessing you are using JSONRPC] to create an object and use toStirng on that.

Eric
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
MY JSONRPC is rusty since I have not done it for a long time, but it should be something like



Eric
Johnny Chan
Greenhorn

Joined: Mar 05, 2009
Posts: 14
If i do that, then i get a "not well-formed" error,



where the arrow is between the " and v.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Is A B C the actual values that you are returning or is there something else in there that is causing the issue?

Also are you serving up the content type header set to text/json or text/javascript?

Eric
Johnny Chan
Greenhorn

Joined: Mar 05, 2009
Posts: 14
Okay, yep, needed to set:


and now its working. thanks very much. i assume i'll need to alter the javascript line


to account for the change to the structure.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You can do something like this:



Eric
Johnny Chan
Greenhorn

Joined: Mar 05, 2009
Posts: 14
Actually i did:


but i see it doesn't take into account the situation where jsonData doesn't exist.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
It is normally a best practice to get to the closest point you can to the data to access it. The whole idea behind that is the looking up data in the chain. With doing object.prop, it has to go down a level each time which adds time. We are talking fractions of milliseconds here. You only really see benefits in very large loops.

Eric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSON Syntax error