Java中json與javaBean幾種互轉(zhuǎn)的講解
一、java普通對象和json字符串的互轉(zhuǎn)
java對象---->json
首先創(chuàng)建一個java對象:
public class Student {
//姓名
private String name;
//年齡
private String age;
//住址
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", address="
+ address + "]";
}
}
現(xiàn)在java對象轉(zhuǎn)換為json形式:
public static void convertObject() {
Student stu=new Student();
stu.setName("JSON");
stu.setAge("23");
stu.setAddress("北京市西城區(qū)");
//1、使用JSONObject
JSONObject json = JSONObject.fromObject(stu);
//2、使用JSONArray
JSONArray array=JSONArray.fromObject(stu);
String strJson=json.toString();
String strArray=array.toString();
System.out.println("strJson:"+strJson);
System.out.println("strArray:"+strArray);
}
定義了一個Student的實體類,然后分別使用了JSONObject和JSONArray兩種方式轉(zhuǎn)化為JSON字符串,下面看打印的結(jié)果:

json-->javabean
上面說明了如何把java對象轉(zhuǎn)化為JSON字符串,下面看如何把JSON字符串格式轉(zhuǎn)化為java對象,
首先需要定義兩種不同格式的字符串,需要使用\對雙引號進(jìn)行轉(zhuǎn)義。
public static void jsonStrToJava(){
//定義兩種不同格式的字符串
String objectStr="{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"北京市西城區(qū)\"}";
String arrayStr="[{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"北京市西城區(qū)\"}]";
//1、使用JSONObject
JSONObject jsonObject=JSONObject.fromObject(objectStr);
Student stu=(Student)JSONObject.toBean(jsonObject, Student.class);
//2、使用JSONArray
JSONArray jsonArray=JSONArray.fromObject(arrayStr);
//獲得jsonArray的第一個元素
Object o=jsonArray.get(0);
JSONObject jsonObject2=JSONObject.fromObject(o);
Student stu2=(Student)JSONObject.toBean(jsonObject2, Student.class);
System.out.println("stu:"+stu);
System.out.println("stu2:"+stu2);
}
運行結(jié)果:

從上面的代碼中可以看出,使用JSONObject可以輕松的把JSON格式的字符串轉(zhuǎn)化為java對象,但是使用JSONArray就沒那么容易了,因為它有“[]”符號,所以我們這里在獲得了JSONArray的對象之后,取其第一個元素即我們需要的一個student的變形,然后使用JSONObject輕松獲得。
二、list和json字符串的互轉(zhuǎn)
下面將list轉(zhuǎn)化為json字符串:
public static void convertListObject() {
Student stu=new Student();
stu.setName("JSON");
stu.setAge("23");
stu.setAddress("北京市西城區(qū)");
Student stu2=new Student();
stu2.setName("JSON2");
stu2.setAge("23");
stu2.setAddress("北京市西城區(qū)");
//注意如果是list多個對象比需要使用JSONArray
JSONArray array=JSONArray.fromObject(stu);
String strArray=array.toString();
System.out.println("strArray:"+strArray);
}
運行結(jié)果為:
strArray:[{"address":"北京市西城區(qū)","name":"JSON","age":"23"},{"address":"北京市西城區(qū)","name":"JSON2","age":"23"}]
如果使用JSONObject進(jìn)行轉(zhuǎn)換會出現(xiàn):
Exception in thread "main" net.sf.json.JSONException: 'object' is an array. Use JSONArray instead
下面將json串轉(zhuǎn)換為list
public static void jsonToList(){
String arrayStr="[{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"北京市西城區(qū)\"},{\"name\":\"JSON2\",\"age\":\"24\",\"address\":\"北京市西城區(qū)\"}]";
//轉(zhuǎn)化為list
List<Student> list2=(List<Student>)JSONArray.toList(JSONArray.fromObject(arrayStr), Student.class);
for (Student stu : list2) {
System.out.println(stu);
}
//轉(zhuǎn)化為數(shù)組
Student[] ss =(Student[])JSONArray.toArray(JSONArray.fromObject(arrayStr),Student.class);
for (Student student : ss) {
System.out.println(student);
}
}
運行結(jié)果為:

三、map和json字符串的互轉(zhuǎn)
map轉(zhuǎn)化為json字符串
public static void mapToJSON(){
Student stu=new Student();
stu.setName("JSON");
stu.setAge("23");
stu.setAddress("中國上海");
Map<String,Student> map=new HashMap<String,Student>();
map.put("first", stu);
//1、JSONObject
JSONObject mapObject=JSONObject.fromObject(map);
System.out.println("mapObject:"+mapObject.toString());
//2、JSONArray
JSONArray mapArray=JSONArray.fromObject(map);
System.out.println("mapArray:"+mapArray.toString());
}
運行結(jié)果:

json字符串轉(zhuǎn)化為map:
public static void jsonToMap(){
String strObject="{\"first\":{\"address\":\"中國上海\",\"age\":\"23\",\"name\":\"JSON\"}}";
//JSONObject
JSONObject jsonObject=JSONObject.fromObject(strObject);
Map map=new HashMap();
map.put("first", Student.class);
//使用了toBean方法,需要三個參數(shù)
MyBean my=(MyBean)JSONObject.toBean(jsonObject, MyBean.class, map);
System.out.println(my.getFirst());
}
注意在轉(zhuǎn)化為map的時候需要創(chuàng)建一個類,類里面需要有first屬性跟json串里面的一樣:
使用toBean()方法是傳入了三個參數(shù),第一個是JSONObject對象,第二個是MyBean.class,第三個是一個Map對象。通過MyBean可以知道此類中要有一個first的屬性,且其類型為Student,要和map中的鍵和值類型對應(yīng),即,first對應(yīng)鍵 first類型對應(yīng)值的類型。
運行結(jié)果:

總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
spring?boot?executable?jar/war?原理解析
spring boot里其實不僅可以直接以 java -jar demo.jar的方式啟動,還可以把jar/war變?yōu)橐粋€可以執(zhí)行的腳本來啟動,比如./demo.jar,這篇文章主要介紹了spring?boot?executable?jar/war?原理,需要的朋友可以參考下2023-02-02
SpringBoot執(zhí)行有返回值的異步任務(wù)問題
這篇文章主要介紹了SpringBoot執(zhí)行有返回值的異步任務(wù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
java線程并發(fā)控制同步工具CountDownLatch
這篇文章主要為大家介紹了java線程并發(fā)控制同步工具CountDownLatch使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
如何使用pipeline和jacoco獲取自動化測試代碼覆蓋率
這篇文章主要介紹了如何使用pipeline和jacoco獲取自動化測試代碼覆蓋率,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11
java集合類arraylist循環(huán)中刪除特定元素的方法
下面小編就為大家?guī)硪黄狫ava集合類ArrayList循環(huán)中刪除特定元素的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11
并行Stream與Spring事務(wù)相遇會發(fā)生什么?
這篇文章主要介紹了并行Stream與Spring事務(wù)相遇會發(fā)生什么?文章主要解決實戰(zhàn)中的Bug及解決方案和技術(shù)延伸,具有一定的參考價值,需要的小伙伴可以參考一下2022-05-05
Spring自帶定時任務(wù)@Scheduled注解實例講解
這篇文章主要介紹了Spring自帶定時任務(wù)@Scheduled注解的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-06-06
MAC 系統(tǒng)如何使用 Sublime Text 2 直接編譯運行 java 代碼
這篇文章主要介紹了MAC 系統(tǒng)如何使用 Sublime Text 2 直接編譯運行 java 代碼,需要的朋友可以參考下2014-10-10

