1

Send JS object to JSP page

I followed this link and found that one of the answer says that we can create a JSON object by using this constructor:

JSONObject jsonObj=new JSONObject(String_to_Be_Parsed);

But when I downloaded the library of JSON and put that jar in my project I found that it supports only two constructors namely:

JSONObject()
JSONObject(boolean) 

Although the documentation in the site of JSON also have a number of constructors including the one I want, JSONObject(String)?

What should I do?

Community
  • 1
  • 1
Ankur Verma
  • 79
  • 2
  • 10

2 Answers2

1

The link provided by the comment to that answer apparently was not the correct source (note that the comment had a different author than the answer). I have added a comment to correct it. The correct source is probably here: http://json.org/java/. (Actually it looks like there are numerous 3rd party implementations and this is just a reference implementation. It looks like it would work, except you have to build your own .jar apparently.)

You can see the JSONObject(String) constructor right in the source for JSONObject.java.

/**
 * Construct a JSONObject from a source JSON text string.
 * This is the most commonly used JSONObject constructor.
 * @param source    A string beginning
 *  with <code>{</code>&nbsp;<small>(left brace)</small> and ending
 *  with <code>}</code>&nbsp;<small>(right brace)</small>.
 * @exception JSONException If there is a syntax error in the source
 *  string or a duplicated key.
 */
public JSONObject(String source) throws JSONException {
    this(new JSONTokener(source));
}
mellamokb
  • 56,094
  • 12
  • 110
  • 136
  • If you look at the docs, that JSONObject doesn't have a JSONObject(String) constructor, either. – Ryan Stewart Oct 07 '11 at 05:27
  • @RyanStewart: [Yes it does.](http://www.json.org/javadoc/org/json/JSONObject.html#JSONObject(java.lang.String)) – mellamokb Oct 07 '11 at 05:36
  • Oops, my bad. I never noticed it hiding among those others. – Ryan Stewart Oct 07 '11 at 05:45
  • Actually I searched the internet and found another jar that is having the number of constructors found in the documentation of JSON Link is this: http://code.google.com/p/wave-robot-java-client/downloads/detail?name=json.jar&can=4&q= So in order to use JSON correctly we have to include two jars namely the one we get from JSON website and one getting from Google website(Link given) While using we have to specifically tell the compiler whose constructor we want to call – Ankur Verma Oct 07 '11 at 06:57
1

The one JSONObject I know with a constructor that accepts a JSON string is in Jettison. It's very handy.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199