網站首頁 前端文檔 正文
JSONObject只是一種數據結構,可以理解為JSON格式的數據結構(key-value
結構),可以使用put方法給json對象添加元素。JSONObject可以很方便的轉換成字符串,也可以很方便的把其他對象轉換成JSONObject對象。
pom:
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency>
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數據結構生成
HashMap<String, Object> zhangsan = 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
ArrayList<Student> studentLsit = 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);
阿里的json很好用,還有一個谷歌Gson也不錯。有興趣的可以看一看
原文鏈接:https://blog.csdn.net/u012448904/article/details/84292821
相關推薦
- 2022-06-04 C#處理類型和二進制數據轉換并提高程序性能_C#教程
- 2022-08-29 Oracle中日期的使用方法實例_oracle
- 2022-07-22 CondaVerificationError:關于conda虛擬環境卸載后導致python版本腐化的
- 2022-01-30 uniapp history路由配置,apache服務器簡單配置(刷新報404解決方法)
- 2023-01-13 Pytorch中關于BatchNorm2d的參數解釋_python
- 2022-12-14 VSCode如何巧用正則表達式快速處理字符段_相關技巧
- 2022-07-04 解決Python保存文件名太長OSError:?[Errno?36]?File?name?too?l
- 2022-03-27 C語言中scanf與scnaf_s函數詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支