0

I have an array of objects in javascript and I know that I need to use json to pass it to server, but I don't know which library I should use. I found in this post, that I have to use JSON.stringify() function; , but there's also a comment that says that JSON.stringify is not a standard function and I should use json2.js instead. What I'd like to know is :

  1. Is there any pre-installed Json library in javascript?

  2. Which is the best Json library I can use in javascript as well as in java.

  3. And... if it´s not too much to ask... where can I get those libraries

thanks beforehand

Community
  • 1
  • 1
eddy
  • 4,373
  • 16
  • 60
  • 94

2 Answers2

1
  1. There is a JSON property in the global object for browsers using the ECMAScript v5 specification (Dec 09).
  2. If it's in both Java and JavaScript then it's not the same library... Personally, I usually use Jackson in Java and ExtJS in Javascript.
  3. Here and here. A much more lightweight js implementation can be found here.
OrangeDog
  • 36,653
  • 12
  • 122
  • 207
  • JSON is supported natively in all modern browsers. See http://en.wikipedia.org/wiki/JavaScript_Object_Notation#Native_JSON. – Zach Dec 08 '10 at 00:16
  • @Zach - Corrected. Assuming a modern browser is not generally valid though. – OrangeDog Dec 08 '10 at 00:19
  • @OrangeDog Sure, it depends on the application though. I agree that for a public-facing website, it probably makes sense to target all browsers out there so as not to lose some percent of possible viewers. For that application, you'd want to use a JSON library to ensure functionality. – Zach Dec 08 '10 at 01:22
  • thanks OrangeDog, using Json2 was easy, but now that I trying to parse the string in Json format I got from the client, I got this error message:"java.lang.NoClassDefFoundError: org/codehaus/jackson/map/ObjectMapper" and this is the code I'm using: ObjectMapper mapper = new ObjectMapper(); List lcd = (List) mapper.readValue(ldc, CompraDetalle.class); – eddy Dec 08 '10 at 16:18
  • @eddy - The error means that the Jackson jars are not on the classpath. – OrangeDog Dec 08 '10 at 17:27
  • @OrangeDog- since you said you usually use Jackon and if it's not too much to ask, could you please see this post? http://stackoverflow.com/questions/4392326/problem-when-trying-to-use-jackson-in-java – eddy Dec 08 '10 at 21:25
1

See http://en.wikipedia.org/wiki/JavaScript_Object_Notation#Using_JSON_in_Ajax and http://en.wikipedia.org/wiki/JavaScript_Object_Notation#Native_JSON.

JSON is supported natively in modern browsers (and in older browsers through eval(), but with possible security and perf implications). http://json.org/ provides a list of JSON libraries for older browsers where native JSON is not supported and the use of eval() is not a good idea.

Zach
  • 7,730
  • 3
  • 21
  • 26