如题所述
开始看网上关于json的例子程序段,实验了一把发现结果和网上例子程序的结果不一致,后来发现使用json需要导入的jar包不全,只导入json-lib-2.2.2-jdk15.jar ,还需以下几个包:commons-beanutils-1.7.0.jar
commons-collections-3.2.jar
commons-lang.jar
commons-logging-1.1.jar
ezmorph-1.0.4.jar
这时在测试得到了想要的结果
Question q2 = new Question();
q2.setId(2);
q2.setDescription("descrsssss");
q2.setPointInfo("pointkkkk");
q2.setType(3);
Question q1 = new Question();
q1.setId(1);
q1.setDescription("descrsssss");
q1.setPointInfo("pointkkkk");
q1.setType(3);
JSONObject jsonObject = new JSONObject().fromObject(q2);
System.out.println(jsonObject);
List list = new ArrayList();
list.add(q1);
list.add(q2);
JSONArray jsonArr= JSONArray.fromObject(list);
System.out.println(jsonArr);
输出:{"description":"descrsssss","id":2,"pointInfo":"pointkkkk","type":3}
[{"description":"descrsssss","id":1,"pointInfo":"pointkkkk","type":3},{"description":"descrsssss","id":2,"pointInfo":"pointkkkk","type":3}]页面
$.post("showQuestions?type="+type,null,callbackShowQuestions,"json");
这时在回调函数的参数data获得的就是json转换后的js对象数组,
以下标方式便可访问数组里的相应对象信息。
commons-collections-3.2.jar
commons-lang.jar
commons-logging-1.1.jar
ezmorph-1.0.4.jar
这时在测试得到了想要的结果
Question q2 = new Question();
q2.setId(2);
q2.setDescription("descrsssss");
q2.setPointInfo("pointkkkk");
q2.setType(3);
Question q1 = new Question();
q1.setId(1);
q1.setDescription("descrsssss");
q1.setPointInfo("pointkkkk");
q1.setType(3);
JSONObject jsonObject = new JSONObject().fromObject(q2);
System.out.println(jsonObject);
List list = new ArrayList();
list.add(q1);
list.add(q2);
JSONArray jsonArr= JSONArray.fromObject(list);
System.out.println(jsonArr);
输出:{"description":"descrsssss","id":2,"pointInfo":"pointkkkk","type":3}
[{"description":"descrsssss","id":1,"pointInfo":"pointkkkk","type":3},{"description":"descrsssss","id":2,"pointInfo":"pointkkkk","type":3}]页面
$.post("showQuestions?type="+type,null,callbackShowQuestions,"json");
这时在回调函数的参数data获得的就是json转换后的js对象数组,
以下标方式便可访问数组里的相应对象信息。
温馨提示:答案为网友推荐,仅供参考