当前位置:首页 » 编程语言 » 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导入你的项目中即可。

热点内容
飞智黑武士手2柄如何连接安卓手机 发布:2025-04-07 07:39:46 浏览:967
手机文件夹如何加密码 发布:2025-04-07 07:33:55 浏览:275
C语言预编译的例子 发布:2025-04-07 07:12:27 浏览:756
软件加密破解 发布:2025-04-07 07:04:34 浏览:835
乌合之众中央编译出版社百度网盘 发布:2025-04-07 06:53:12 浏览:806
美颜算法磨皮 发布:2025-04-07 06:38:37 浏览:5
手机视频源码下载 发布:2025-04-07 06:34:21 浏览:740
安卓手机投屏到电视上怎么切换 发布:2025-04-07 06:22:02 浏览:379
支票密码多少位 发布:2025-04-07 06:16:09 浏览:282
php观察 发布:2025-04-07 06:16:06 浏览:868