將JSON字符串?dāng)?shù)組轉(zhuǎn)對象集合方法步驟
1、JSON字符串?dāng)?shù)組轉(zhuǎn)對象集合
String json ="json數(shù)組數(shù)據(jù)"; JSONArray res = JSON.getJSONArray(json); //用json的方法toJavaList,參數(shù)放入想轉(zhuǎn)的集合對象就可以了 List<MonthTaskRes> monthTaskRes = res.toJavaList(MonthTaskRes.class);
2、將java對象轉(zhuǎn)換為json字符串
利用json與java對象之間可以相互轉(zhuǎn)換的方式進(jìn)行存值和取值 String s = JacksonUtils.getInstance().writeValueAsString(user); System.out.println(“對象轉(zhuǎn)化字符串:”+s); User user1 = JacksonUtils.getInstance().readValue(s, User.class); System.out.println(user1 );
3、json字符串與Java對象的轉(zhuǎn)換
a> 把Java對象列表轉(zhuǎn)換成json對象數(shù)組,并轉(zhuǎn)為字符串 JSONArray array=JSONArray.fromObject(list); String jsonString = array.toString(); b> 把Java對象轉(zhuǎn)換成json對象,并轉(zhuǎn)化成字符串 JSONObject obj = JSONObject.fromObject(user); Log4jInit.ysulogger.debug(obj.toString()); c> 把json字符串轉(zhuǎn)換成Java對象數(shù)組 JSONArray json=JSONArray.fromObject(jsonString);//jsonString字符串?dāng)?shù)組 List<User> list =(List<User>) JSONArray .toCollection(json,User.class); d> 把字符串轉(zhuǎn)換成java對象 JSONObject obj = JSONObject.fromObject(jsonString);/jsonString字符串 User user= (User)JSONObject.toBean(obj,User.class);
4、步驟
1、引入jar 包
<!-- fastjson依賴 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.76</version> </dependency>
2、Java對象轉(zhuǎn)成JSON格式
import com.alibaba.fastjson.JSON; import com.example.study.entity.Student; public class JsonStudy { public static void main(String[] args) { Student student = new Student("123","張三",18); //直接輸出,結(jié)果為:Student(id=123, name=張三, age=18) System.out.println(student); //轉(zhuǎn)換為JSON格式輸出,以下兩種方法只有返回值不同 Object objectJson = JSON.toJSON(student); String stringJSON = JSON.toJSONString(student); //結(jié)果為:{"name":"張三","id":"123","age":18} System.out.println(objectJson); //結(jié)果為:{"name":"張三","id":"123","age":18} System.out.println(stringJSON); } }
3、JSON格式字符串轉(zhuǎn)換成Java對象
3.1、直接將JSON字符串轉(zhuǎn)換成Java對象
import com.alibaba.fastjson.JSONObject; import com.example.study.entity.Student; public class JsonStudy { public static void main(String[] args) { //定義一個student類型的JSON字符串 String json = "{\"name\":\"張三\",\"id\":\"123\",\"age\":18}"; //將這個JSON字符串轉(zhuǎn)換成Student對象 Student student = JSONObject.parseObject(json, Student.class); //輸出結(jié)果為:Student(id=123, name=張三, age=18) System.out.println(student); //輸出結(jié)果為:張三 System.out.println(student.getName()); } }
3.2、先將JSON字符串轉(zhuǎn)換成JSON對象,再轉(zhuǎn)換成Java對象
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.example.study.entity.Student; public class JsonStudy { public static void main(String[] args) { //定義一個student類型的JSON字符串 String json = "{\"name\":\"張三\",\"id\":\"123\",\"age\":18}"; //將這個JSON字符串轉(zhuǎn)換成JSON對象 JSONObject jsonObject = JSONObject.parseObject(json); //將JSON對象轉(zhuǎn)換成Java對象 Student student1 = JSONObject.toJavaObject(jsonObject, Student.class); Student student2 = JSON.toJavaObject(jsonObject, Student.class); //輸出結(jié)果為:Student(id=123, name=張三, age=18) System.out.println(student1); //輸出結(jié)果為:張三 System.out.println(student1.getName()); //輸出結(jié)果為:Student(id=123, name=張三, age=18) System.out.println(student2); //輸出結(jié)果為:張三 System.out.println(student2.getName()); }
3.3、如果JSON字符串是一個JSON數(shù)組,并且數(shù)組里面存放的同一種類型的對象,可以將這個JSON數(shù)組轉(zhuǎn)換成Java的List對象;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.example.study.entity.Student; import java.util.ArrayList; import java.util.List; public class JsonStudy { public static void main(String[] args) { //new一個list對象 List<Student> studentList = new ArrayList<>(); studentList.add(new Student("111","張三",18)); studentList.add(new Student("222","李四",20)); studentList.add(new Student("333","王五",23)); //獲取list的JSON數(shù)組形式字符串 String listJsonString = JSON.toJSONString(studentList); //結(jié)果為:[{"age":18,"id":"111","name":"張三"},{"age":20,"id":"222","name":"李四"},{"age":23,"id":"333","name":"王五"}] System.out.println(listJsonString); //將JSON字符串轉(zhuǎn)換成List對象,List里面放的Student對象 List<Student> students = JSONObject.parseArray(listJsonString, Student.class); //結(jié)果為:[Student(id=111, name=張三, age=18), Student(id=222, name=李四, age=20), Student(id=333, name=王五, age=23)] System.out.println(students); //結(jié)果為:3 System.out.println(students.size()); //結(jié)果為:Student(id=333, name=王五, age=23) System.out.println(students.get(2)); } }
總結(jié)
到此這篇關(guān)于將JSON字符串?dāng)?shù)組轉(zhuǎn)對象集合的文章就介紹到這了,更多相關(guān)JSON字符串?dāng)?shù)組轉(zhuǎn)對象集合內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Mysql如何獲取json字符串/數(shù)組的值
- JavaScript中判斷某個字符串、數(shù)組等是否包含某個值的五種方法
- JS字符串轉(zhuǎn)換為數(shù)組的4 個方法示例小結(jié)
- 利用js判斷數(shù)據(jù)是否是數(shù)組或字符串的常見方法
- JavaScript將數(shù)組轉(zhuǎn)為對象與JSON對象字符串轉(zhuǎn)數(shù)組方法詳解
- JavaScript實(shí)現(xiàn)字符串轉(zhuǎn)數(shù)組的6種方法總結(jié)
- 如何將JSON字符串?dāng)?shù)組轉(zhuǎn)對象集合
- JS數(shù)組轉(zhuǎn)字符串實(shí)現(xiàn)方法解析
- JS字符串和數(shù)組如何實(shí)現(xiàn)相互轉(zhuǎn)化
- JavaScript 中字符串和數(shù)組的概念解析與多角度對比區(qū)分
相關(guān)文章
MybatisPlus 主鍵策略的幾種實(shí)現(xiàn)方法
MybatisPlus-Plus支持多種主鍵生成策略,可以通過@TableId注解的type屬性配置,主要策略包括AUTO、INPUT、ASSING_ID、ASSING_UUID和NONE,每種策略適用于不同的場景,下面就來介紹一下2024-10-10基于apache poi根據(jù)模板導(dǎo)出excel的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄赼pache poi根據(jù)模板導(dǎo)出excel的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06Java怎樣創(chuàng)建集合才能避免造成內(nèi)存泄漏你了解嗎
內(nèi)存泄漏是指無用對象持續(xù)占有內(nèi)存或無用對象的內(nèi)存得不到及時釋放,從而造成內(nèi)存空間的浪費(fèi)稱為內(nèi)存泄漏。長生命周期的對象持有短生命周期對象的引用就很可能發(fā)生內(nèi)存泄漏,盡管短生命周期對象已經(jīng)不再需要,但是因?yàn)殚L生命周期持有它的引用而導(dǎo)致不能被回收2021-09-09Flink自定義Sink端實(shí)現(xiàn)過程講解
這篇文章主要介紹了Flink自定義Sink端實(shí)現(xiàn)過程,在Fink官網(wǎng)中sink端只是給出了常規(guī)的write api.在我們實(shí)際開發(fā)場景中需要將flink處理的數(shù)據(jù)寫入kafka,hbase kudu等外部系統(tǒng)2023-01-01java 多線程的幾種實(shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了java 多線程的幾種實(shí)現(xiàn)方法總結(jié)的相關(guān)資料,希望通過本文能幫助到大家,讓大家掌握java多線程的知識,需要的朋友可以參考下2017-10-10使用Netty快速實(shí)現(xiàn)一個群聊功能的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用?Netty?框架開發(fā)一個?WebSocket?服務(wù)端,從而實(shí)現(xiàn)一個簡單的在線聊天功能,感興趣的小伙伴可以了解下2023-11-11