开发者

Passing JSON object

开发者 https://www.devze.com 2023-04-11 14:24 出处:网络
Send JS object to JSP page I followed this link and found that one of the answer says that we can createa JSON object by using this construc开发者_如何学Gotor:

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 construc开发者_如何学Gotor:

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?


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));
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号