導航:首頁 > 編程語言 > 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相關的資料

熱點內容
四川補貼認證下載什麼app 瀏覽:858
android設計風格 瀏覽:426
視頻不支持我的加密 瀏覽:342
布包pdf 瀏覽:267
程序員錄制課程表 瀏覽:626
eclipsephp斷點調試 瀏覽:895
虛擬成交量指標源碼 瀏覽:838
什麼APP有背單詞小組 瀏覽:43
蘋果2g視頻怎麼加密 瀏覽:204
人工智慧程序員和古典錄音師相遇 瀏覽:415
國產伺服器是怎麼來的 瀏覽:116
蓄勢待發源碼 瀏覽:458
伺服器如何清理log文件 瀏覽:835
javaawtfont 瀏覽:627
php企業站後台 瀏覽:417
日企程序員招聘 瀏覽:113
伺服器中毒網頁投放廣告怎麼辦 瀏覽:709
安卓快閃記憶體掉速是什麼原因 瀏覽:409
fcfs調度演算法代碼 瀏覽:686
應用加密安全提示問題如何重設 瀏覽:54