如何将Java对象转换成json格式,如何将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对象数组,
以下标方式便可访问数组里的相应对象信息。
温馨提示:内容为网友见解,仅供参考
第1个回答  2016-12-30
这个你可以用别人造好的轮子,google的gson,其他比较流行的json框架一般都实现好了实体类和json之间的转换。
相似回答