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

热点内容
我的世界服务器等级如何升 发布:2025-01-15 12:45:55 浏览:687
c语言程序填空题 发布:2025-01-15 12:45:53 浏览:543
怎么配置氯化钠浓度 发布:2025-01-15 12:34:06 浏览:205
4000除以125简便算法 发布:2025-01-15 12:27:41 浏览:463
源码商用 发布:2025-01-15 12:26:54 浏览:74
价钱算法 发布:2025-01-15 12:26:03 浏览:400
苹果手机安卓功能在哪里 发布:2025-01-15 12:21:34 浏览:786
服务器地址端口名称怎么找 发布:2025-01-15 12:15:32 浏览:705
怎么把电脑程序改安卓 发布:2025-01-15 12:13:52 浏览:576
安卓如何设置格子 发布:2025-01-15 12:02:11 浏览:936