传递JSON对象

问题描述:

Send JS object to JSP page传递JSON对象

我跟着这个链接,发现了答案的人说,我们可以通过使用此构造方法创建一个JSON对象:

JSONObject jsonObj=new JSONObject(String_to_Be_Parsed); 

但是当我下载了JSON的图书馆并把那个罐子在项目中,我发现,它仅支持两个构造,即:

JSONObject() 
JSONObject(boolean) 

虽然JSON的站点上的文档也有一些构造包括我想要的那个,JSONObject(String)

我该怎么办?

该评论对该答案提供的链接显然不是正确的来源(注意该评论与答案有不同的作者)。我添加了一条评论来纠正它。正确的来源可能在这里:http://json.org/java/。 (实际上它看起来像有很多第三方实现,这只是一个参考实现,看起来它可以工作,除非你必须显然构建自己的.jar。)

您可以在右边看到JSONObject(String)构造函数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)); 
} 
+0

如果你看看文档,那JSONObject也没有JSONObject(String)构造函数。 –

+0

http://www.json.org/javadoc/org/json/JSONObject.html 看看这 –

+0

@RyanStewart:[是的确实。](http://www.json.org/javadoc/org/json /JSONObject.html#JSONObject(java.lang.String)) – mellamokb

的一个JSONObject的我知道有一个接受JSON字符串是Jettison构造。这非常方便。