JSONObject怎么用
发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,这篇文章将为大家详细讲解有关JSONObject怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。JSONObject只是一种数据结构,可以理解为JSON格式的数
千家信息网最后更新 2024年09月22日JSONObject怎么用
这篇文章将为大家详细讲解有关JSONObject怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
JSONObject只是一种数据结构,可以理解为JSON格式的数据结构(key-value
结构),可以使用put方法给json对象添加元素。JSONObject可以很方便的转换成字符串,也可以很方便的把其他对象转换成JSONObject对象。
pom:
com.alibaba fastjson 1.2.28
1.通过原生生成json数据格式。
JSONObject zhangsan = new JSONObject(); try { //添加 zhangsan.put("name", "张三"); zhangsan.put("age", 18.4); zhangsan.put("birthday", "1900-20-03"); zhangsan.put("majar", new String[] {"哈哈","嘿嘿"}); zhangsan.put("null", null); zhangsan.put("house", false); System.out.println(zhangsan.toString()); } catch (JSONException e) { e.printStackTrace(); }
2.通过hashMap数据结构生成
HashMapzhangsan = new HashMap<>(); zhangsan.put("name", "张三"); zhangsan.put("age", 18.4); zhangsan.put("birthday", "1900-20-03"); zhangsan.put("majar", new String[] {"哈哈","嘿嘿"}); zhangsan.put("null", null); zhangsan.put("house", false); System.out.println(new JSONObject(zhangsan).toString());
3.通过实体生成
Student student = new Student(); student.setId(1); student.setAge("20"); student.setName("张三"); //生成json格式 System.out.println(JSON.toJSON(student)); //对象转成string String stuString = JSONObject.toJSONString(student);
4.JSON字符串转换成JSON对象
String studentString = "{\"id\":1,\"age\":2,\"name\":\"zhang\"}"; //JSON字符串转换成JSON对象JSONObject jsonObject1 = JSONObject.parseObject(stuString); System.out.println(jsonObject1);
5.list对象转listJson
ArrayListstudentLsit = new ArrayList<>(); Student student1 = new Student(); student1.setId(1); student1.setAge("20"); student1.setName("asdasdasd"); studentLsit.add(student1); Student student2 = new Student(); student2.setId(2); student2.setAge("20"); student2.setName("aaaa:;aaa"); studentLsit.add(student2); //list转json字符串 String string = JSON.toJSON(studentLsit).toString(); System.out.println(string); //json字符串转listJson格式 JSONArray jsonArray = JSONObject.parseArray(string); System.out.println(jsonArray);
关于"JSONObject怎么用"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
对象
字符
字符串
数据
格式
结构
生成
数据结构
篇文章
张三
更多
不错
实用
元素
内容
只是
实体
文章
方法
知识
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
公安部网络安全局认证
人人共享网络安全
蛋白组数据库怎么下载
临汾 软件开发
福建企业软件开发供应
重庆工商大学网络安全学院怎样
铁路网络安全生产
国内网络安全事件
关于网络安全提出建议和意见
软件开发流程知乎
上网为什么需要服务器
全国网络安全 与信息化
如何查询服务器内存使用量
数据库清空表
恒生互联网科技指数百度百科
数据库间的同步
医院设备网络安全标准
网络安全的攻防体系具体
工银瑞信软件开发
软件开发解决方案项目多的弊端
读网络安全选课要求
软件开发一般是几个阶段
软件开发简历上的简介怎么写
排课表软件开发
会计购买服务器托盘分录
计算机应用的数据库
天戏网络安全工程师
新时达服务器故障大全
蒙古国网络安全
地产公司成本大数据库