导航:首页 > 编程语言 > 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相关的资料

热点内容
linuxvi保存文件 浏览:533
把视频打包出文件夹是什么意思 浏览:443
如何在藏书馆app上注销账号 浏览:823
51单片机架构 浏览:895
安卓下载东西怎么弄 浏览:520
我的世界服务器地址13 浏览:309
机修编程原理 浏览:720
手机点开app反应慢是哪里的问题 浏览:772
数控铣床g代码编程图案 浏览:129
lan是指什么服务器 浏览:769
php匹配手机号 浏览:444
火狐app拦截窗口如何解除 浏览:902
javaapichm下载 浏览:162
如何用代理服务器玩cf 浏览:999
java对象转jsonobject 浏览:371
怎么删除app里的更新提示 浏览:422
日月单片机 浏览:152
airports在安卓上如何查看电量 浏览:252
北京回收全新服务器硬盘云主机 浏览:517
php空间搭建ss 浏览:507