導航:首頁 > 編程語言 > java對象轉jsonobject

java對象轉jsonobject

發布時間:2025-01-11 21:26:51

A. java中如何將對象轉成json格式字元串

用Gson轉換就行了,需要下載jar包

例子:

Personperson=newPerson();
Gsongson=newGson();
Stringjson=gson.toJson(person);


B. 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}]}

C. Java怎麼把Map轉換成JSON數據

導入import org.json.JSONObject;包
Map<String, Object> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
// 將Map對象轉換為JSONObject
JSONObject jsonObject = new JSONObject(map);

閱讀全文

與java對象轉jsonobject相關的資料

熱點內容
androidio流 瀏覽:797
伺服器怎麼改nvr 瀏覽:937
真空壓縮袋怎樣打開 瀏覽:80
證券市場基礎知識pdf2012 瀏覽:720
mastercam自動編程視頻 瀏覽:9
如何得知加密類型 瀏覽:40
單片機匯編讓主程序循環執行 瀏覽:371
電訊APP是干什麼的 瀏覽:532
程序員互相之間不能透露薪水 瀏覽:972
美團伺服器是什麼意思 瀏覽:416
周末程序員吃什麼 瀏覽:996
解壓的pdf格式怎麼轉換 瀏覽:228
ad輸出pdf 瀏覽:306
dhcp伺服器如何部署 瀏覽:508
如何追女生app 瀏覽:432
阿里雲域名綁定香港伺服器 瀏覽:551
java文本文件壓縮 瀏覽:531
心靈解壓大笑視頻 瀏覽:600
東學西學單片機 瀏覽:671
phpstudy做伺服器如何使用 瀏覽:952