分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

JSONObject.fromObject--JSON与对象的转换

发布时间:2023-09-06 02:32责任编辑:白小东关键词:暂无标签

1. List集合转换成json代码

List list = new ArrayList();

list.add( "first" );

list.add( "second" );

JSONArray jsonArray2 = JSONArray.fromObject( list );

2. Map集合转换成json代码

Map map = new HashMap();

map.put("name", "json");

map.put("bool", Boolean.TRUE);

map.put("int", new Integer(1));

map.put("arr", new String[] { "a", "b" });

map.put("func", "function(i){ return this.arr[i]; }");

JSONObject json = JSONObject.fromObject(map);

3. Bean转换成json代码

JSONObject jsonObject = JSONObject.fromObject(new JsonBean());

4. 数组转换成json代码

boolean[] boolArray = new boolean[] { true, false, true };

JSONArray jsonArray1 = JSONArray.fromObject(boolArray);

5. 一般数据转换成json代码

JSONArray jsonArray3 = JSONArray.fromObject("[‘json‘,‘is‘,‘easy‘]" );

List<Role> roleList = roleService.selectRole(conditions);
if(roleList.size()>0)
{
Role role = roleList.get(0);
role.setAddOrEdit("update");
result = JSONObject.fromObject(role);
}

return : {addOrEdit: "update", createBy: "admin", createDate: {…}, roleId: "SALES_55b1dcd69aa64be6845bafef1e04707a", roleName: "test1", …}

JSONObject.fromObject--JSON与对象的转换

原文地址:https://www.cnblogs.com/Yusco/p/10364462.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved