Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSON Syntax error

 
Johnny Chan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you not think that showing us the parsing code might be helpful?
 
Johnny Chan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the main page calls getCBParameter,


 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, its the only thing being returned by the server.
 
Johnny Chan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does not look anything like I posted.

What is generating the serverside JSON?

Eric

 
Johnny Chan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So the response here is:


and the syntax error is pointing to the {" in characters 2 - 3.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MY JSONRPC is rusty since I have not done it for a long time, but it should be something like



Eric
 
Johnny Chan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i do that, then i get a "not well-formed" error,



where the arrow is between the " and v.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do something like this:



Eric
 
Johnny Chan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i did:


but i see it doesn't take into account the situation where jsonData doesn't exist.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic