我从javascript里通过异步请求传到Action里一组json格式的数据。那么在java里该如何解析呢?StringBuffer jb = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null)
jb.append(line);
} catch (Exception e) { //report an error }JSONObject jsonObject = new JSONObject(jb.toString());这是我从ibm的资料库里找到的解析方法。可是在我写的Action里不能解析。说没有JSONObject(String)的构造函数!
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null)
jb.append(line);
} catch (Exception e) { //report an error }JSONObject jsonObject = new JSONObject(jb.toString());这是我从ibm的资料库里找到的解析方法。可是在我写的Action里不能解析。说没有JSONObject(String)的构造函数!
解决方案 »
- struts验证框架 validator 输出的格式
- javaee用XFire搭的web service 框架,可以用js来请求吗?
- 为什么大家都说在session里面传数据不好呢?【求助】
- (求高人解答)ExtJS 将一个gridPanel放到tablePanel时报错
- 急求JSP中相对路径问题
- 版主手下留情:个人感觉csdn community需要改进的地方
- hibernate问题--高手帮帮忙!!!!
- 过滤器的问题~~急~~~~~~~
- WebLogic安装、项目部署求详解!!!!!!!!!!!!!!急
- html页面在tomcat下无法显示中文
- 一个新手的问题
- 高手请赐教,出现java.lang.NoClassDefFoundError: javax/servlet/jsp/PageContext的错误
作者:半精灵 2008-01-03 15:03:26
标签:
ajax
json
java
it
在www.json.org上公布了很多Java下的json解析工具,其中org.json和json-lib比较简单,两者使用上差不多。下面两段源代码是分别使用这两个工具解析和构造JSON的演示程序。
这是使用json-lib的程序:
import java.util.HashMap;
import java.util.Map;import net.sf.json.JSONObject;public class Test { public static void main(String[] args) {
String json = "{\"name\":\"reiz\"}";
JSONObject jsonObj = JSONObject.fromObject(json);
String name = jsonObj.getString("name");
jsonObj.put("initial", name.substring(0, 1).toUpperCase()); String[] likes = new String[] { "JavaScript", "Skiing", "Apple Pie" };
jsonObj.put("likes", likes); Map<String, String> ingredients = new HashMap<String, String>();
ingredients.put("apples", "3kg");
ingredients.put("sugar", "1kg");
ingredients.put("pastry", "2.4kg");
ingredients.put("bestEaten", "outdoors");
jsonObj.put("ingredients",ingredients);
System.out.println(jsonObj);
}
}
这是使用org.json的程序:
import java.util.HashMap;
import java.util.Map;import org.json.JSONException;
import org.json.JSONObject;public class Test { public static void main(String[] args) throws JSONException {
String json = "{\"name\":\"reiz\"}";
JSONObject jsonObj = new JSONObject(json);
String name = jsonObj.getString("name"); jsonObj.put("initial", name.substring(0, 1).toUpperCase()); String[] likes = new String[] { "JavaScript", "Skiing", "Apple Pie" };
jsonObj.put("likes", likes); Map<String, String> ingredients = new HashMap<String, String>();
ingredients.put("apples", "3kg");
ingredients.put("sugar", "1kg");
ingredients.put("pastry", "2.4kg");
ingredients.put("bestEaten", "outdoors");
jsonObj.put("ingredients", ingredients);
System.out.println(jsonObj); System.out.println(jsonObj);
}
}
两者的使用几乎是相同的,但org.json比json-lib要轻量得多,前者没有任何依赖,而后者要依赖ezmorph和commons的lang、logging、beanutils、collections等组件。
jsonObject=jsonObject(jb.toString());
at net.sf.json.JSONObject.fromObject(JSONObject.java:157)
at net.sf.json.JSONObject.fromObject(JSONObject.java:145)
at com.siyn.json.Json_Test.main(Json_Test.java:15)
我运行的程序出现上面的错误 是怎么回事呀 谢谢指教