Java中json與javaBean幾種互轉(zhuǎn)的講解
一、java普通對(duì)象和json字符串的互轉(zhuǎn)
java對(duì)象---->json
首先創(chuàng)建一個(gè)java對(duì)象:
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對(duì)象轉(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);
}
定義了一個(gè)Student的實(shí)體類,然后分別使用了JSONObject和JSONArray兩種方式轉(zhuǎn)化為JSON字符串,下面看打印的結(jié)果:

json-->javabean
上面說(shuō)明了如何把java對(duì)象轉(zhuǎn)化為JSON字符串,下面看如何把JSON字符串格式轉(zhuǎn)化為java對(duì)象,
首先需要定義兩種不同格式的字符串,需要使用\對(duì)雙引號(hào)進(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的第一個(gè)元素
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);
}
運(yùn)行結(jié)果:

從上面的代碼中可以看出,使用JSONObject可以輕松的把JSON格式的字符串轉(zhuǎn)化為java對(duì)象,但是使用JSONArray就沒(méi)那么容易了,因?yàn)樗小癧]”符號(hào),所以我們這里在獲得了JSONArray的對(duì)象之后,取其第一個(gè)元素即我們需要的一個(gè)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多個(gè)對(duì)象比需要使用JSONArray
JSONArray array=JSONArray.fromObject(stu);
String strArray=array.toString();
System.out.println("strArray:"+strArray);
}
運(yùn)行結(jié)果為:
strArray:[{"address":"北京市西城區(qū)","name":"JSON","age":"23"},{"address":"北京市西城區(qū)","name":"JSON2","age":"23"}]
如果使用JSONObject進(jìn)行轉(zhuǎn)換會(huì)出現(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);
}
}
運(yùn)行結(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("中國(guó)上海");
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());
}
運(yùn)行結(jié)果:

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

總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- java對(duì)象與json對(duì)象間的相互轉(zhuǎn)換的方法
- JAVA中JSONObject對(duì)象和Map對(duì)象之間的相互轉(zhuǎn)換
- java實(shí)現(xiàn)Xml與json之間的相互轉(zhuǎn)換操作示例
- Java實(shí)現(xiàn)Json字符串與Object對(duì)象相互轉(zhuǎn)換的方式總結(jié)
- Java代碼實(shí)現(xiàn)Map和Object互轉(zhuǎn)及Map和Json互轉(zhuǎn)
- JSON的String字符串與Java的List列表對(duì)象的相互轉(zhuǎn)換
- Spring?Boot開(kāi)發(fā)時(shí)Java對(duì)象和Json對(duì)象之間的轉(zhuǎn)換
相關(guān)文章
詳解servlet調(diào)用的幾種簡(jiǎn)單方式總結(jié)
這篇文章主要介紹了詳解servlet調(diào)用的幾種簡(jiǎn)單方式總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Java基于二叉查找樹(shù)實(shí)現(xiàn)排序功能示例
這篇文章主要介紹了Java基于二叉查找樹(shù)實(shí)現(xiàn)排序功能,結(jié)合實(shí)例形式分析了Java二叉查找樹(shù)的定義、遍歷及排序等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Java從內(nèi)存角度帶你理解數(shù)組名實(shí)質(zhì)是個(gè)地址的論述
這篇文章主要介紹了Java如何從內(nèi)存解析的角度理解“數(shù)組名實(shí)質(zhì)是一個(gè)地址”,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-09-09
詳解Java?POI?excel自定義設(shè)置單元格格式
這篇文章主要介紹了Java?POI?excel設(shè)置單元格格式,自定義設(shè)置,設(shè)置單元格格式:來(lái)源_formats,更多數(shù)據(jù)類型從formats里面發(fā)現(xiàn),需要的朋友可以參考下2024-01-01
Java觀察者模式之實(shí)現(xiàn)對(duì)象間的一對(duì)多依賴
這篇文章主要介紹了Java觀察者模式之實(shí)現(xiàn)對(duì)象間的一對(duì)多依賴的方法,Java觀察者模式是一種行為型設(shè)計(jì)模式,用于實(shí)現(xiàn)對(duì)象之間的消息傳遞和通信,文中有詳細(xì)的實(shí)現(xiàn)步驟和代碼示例,,需要的朋友可以參考下2023-05-05
SpringCloud基于SpringAMQP實(shí)現(xiàn)消息隊(duì)列及原理解析
Spring AMQP作為Spring框架的一部分,是一套用于支持高級(jí)消息隊(duì)列協(xié)議(AMQP)的工具,這篇文章主要介紹了SpringCloud基于SpringAMQP實(shí)現(xiàn)消息隊(duì)列及原理解析,需要的朋友可以參考下2024-04-04

