當前位置:首頁 » 編程語言 » 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 10:54:13 瀏覽:228
什麼是cf腳本 發布:2024-11-25 10:51:48 瀏覽:920
存儲台設計 發布:2024-11-25 10:40:04 瀏覽:668
如何查看自己電腦的所有配置 發布:2024-11-25 10:14:02 瀏覽:771
java編譯器偽編譯指什麼 發布:2024-11-25 10:08:53 瀏覽:961
amax伺服器默認地址 發布:2024-11-25 10:07:20 瀏覽:318
甘肅省浪潮伺服器雲伺服器 發布:2024-11-25 10:07:17 瀏覽:522
android手環 發布:2024-11-25 10:03:55 瀏覽:163
如何將安卓機設置為蘋果機 發布:2024-11-25 09:41:24 瀏覽:970
伺服器屏蔽一段ip 發布:2024-11-25 08:52:06 瀏覽:100