当前位置:首页 » 编程语言 » json字符串java

json字符串java

发布时间: 2024-08-24 18:12:05

java如何返回json格式

例如:
Student st1 = new Student(1, "dg", 18, new Date());
Student st2 = new Student(2, "dg", 18, new Date());
Student st3 = new Student(3, "dg", 18, new Date());
Student st4 = new Student(4, "dg", 18, new Date());
Student st5 = new Student(5, "dg", 18, new Date());
List li = new ArrayList();
JSONObject JO1 = new JSONObject(st1);
JSONObject JO2 = new JSONObject(st2);
JSONObject JO3 = new JSONObject(st3);
JSONObject JO4 = new JSONObject(st4);
JSONObject JO5 = new JSONObject(st5);
li.add(JO1);
li.add(JO2);
li.add(JO3);
li.add(JO4);
li.add(JO5);
JSONArray Ja = new JSONArray(li);
Map ma = new HashMap();
ma.put("Result", "OK");
ma.put("Records", Ja);
JSONObject js = new JSONObject(ma);
out.print(js);

返回结果:

{"Result":"OK","Records":[{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":1},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":2},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":3},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":4},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":5}]}

⑵ json字符串怎么转换为java对象

可以使用jackson的 new ObjectMapper().readValue(String, bean.class);x0dx0a或明竖者烂漏使用 JSONObject.toBean(String,bean.class);x0dx0a都可以实现json转化为beanx0dx0a而 jackson 或者 JSONObject 的jar包网上均有下载,把下载的饥槐烂jar导入你的项目中即可。

热点内容
访问内网计算机 发布:2024-11-25 12:37:53 浏览:968
苹果文件夹名字大全 发布:2024-11-25 12:28:03 浏览:589
ipad上b站缓存分屏 发布:2024-11-25 12:27:59 浏览:980
数据库日志文件查看 发布:2024-11-25 12:17:28 浏览:47
腾讯视频明明缓存了看不了 发布:2024-11-25 12:08:01 浏览:181
存储区域网络分布 发布:2024-11-25 12:07:12 浏览:388
安卓怎么用汤不热 发布:2024-11-25 11:49:06 浏览:843
java线程调用 发布:2024-11-25 11:42:45 浏览:844
识地图算法 发布:2024-11-25 11:31:10 浏览:20
杨辉三角c语言程序一维数组 发布:2024-11-25 11:30:30 浏览:214