欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

將JSON字符串?dāng)?shù)組轉(zhuǎn)對象集合方法步驟

 更新時間:2023年08月30日 10:34:36   作者:broadenBlueSea  
這篇文章主要給大家介紹了關(guān)于將JSON字符串?dāng)?shù)組轉(zhuǎn)對象集合的方法步驟,文中通過代碼示例介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考借鑒價值,需要的朋友可以參考下

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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MybatisPlus 主鍵策略的幾種實(shí)現(xiàn)方法

    MybatisPlus 主鍵策略的幾種實(shí)現(xiàn)方法

    MybatisPlus-Plus支持多種主鍵生成策略,可以通過@TableId注解的type屬性配置,主要策略包括AUTO、INPUT、ASSING_ID、ASSING_UUID和NONE,每種策略適用于不同的場景,下面就來介紹一下
    2024-10-10
  • Java中工具Jstack的使用實(shí)例

    Java中工具Jstack的使用實(shí)例

    jstack用于生成java虛擬機(jī)當(dāng)前時刻的線程快照,下面這篇文章主要給大家介紹了關(guān)于Java中工具Jstack使用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • 基于apache poi根據(jù)模板導(dǎo)出excel的實(shí)現(xiàn)方法

    基于apache poi根據(jù)模板導(dǎo)出excel的實(shí)現(xiàn)方法

    下面小編就為大家?guī)硪黄赼pache poi根據(jù)模板導(dǎo)出excel的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • 深入探究如何使用Java編寫MapReduce程序

    深入探究如何使用Java編寫MapReduce程序

    MapReduce是一種用于處理大規(guī)模數(shù)據(jù)集的并行編程模型,其特點(diǎn)高效性和可擴(kuò)展性,在本文中,我們將深入了解MapReduce,并使用Java編寫一個簡單的MapReduce程序,需要的朋友可以參考下
    2023-05-05
  • Java怎樣創(chuàng)建集合才能避免造成內(nèi)存泄漏你了解嗎

    Java怎樣創(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-09
  • 基于java中的流程控制語句總結(jié)(必看篇)

    基于java中的流程控制語句總結(jié)(必看篇)

    下面小編就為大家?guī)硪黄趈ava中的流程控制語句總結(jié)(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Flink自定義Sink端實(shí)現(xiàn)過程講解

    Flink自定義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-01
  • Java遍歷起止日期中間的所有日期操作

    Java遍歷起止日期中間的所有日期操作

    這篇文章主要介紹了Java遍歷起止日期中間的所有日期操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • java 多線程的幾種實(shí)現(xiàn)方法總結(jié)

    java 多線程的幾種實(shí)現(xiàn)方法總結(jié)

    這篇文章主要介紹了java 多線程的幾種實(shí)現(xiàn)方法總結(jié)的相關(guān)資料,希望通過本文能幫助到大家,讓大家掌握java多線程的知識,需要的朋友可以參考下
    2017-10-10
  • 使用Netty快速實(shí)現(xiàn)一個群聊功能的示例詳解

    使用Netty快速實(shí)現(xiàn)一個群聊功能的示例詳解

    這篇文章主要為大家詳細(xì)介紹了如何利用?Netty?框架開發(fā)一個?WebSocket?服務(wù)端,從而實(shí)現(xiàn)一個簡單的在線聊天功能,感興趣的小伙伴可以了解下
    2023-11-11

最新評論