在開發(fā)過程中,經(jīng)常需要和別的系統(tǒng)交換數(shù)據(jù),數(shù)據(jù)交換的格式有XML、JSON等,JSON作為一個輕量級的數(shù)據(jù)格式比xml效率要高,XML需要很多的標簽,這無疑占據(jù)了網(wǎng)絡流量,JSON在這方面則做的很好,下面先看下JSON的格式,
JSON可以有兩種格式,一種是對象格式的,另一種是數(shù)組對象,
{"name":"JSON","address":"北京市西城區(qū)","age":25}//JSON的對象格式的字符串
[{"name":"JSON","address":"北京市西城區(qū)","age":25}]//數(shù)據(jù)對象格式
從上面的兩種格式可以看出對象格式和數(shù)組對象格式唯一的不同則是在對象格式的基礎上加上了[],再來看具體的結構,可以看出都是以鍵值對的形式出現(xiàn)的,中間以英文狀態(tài)下的逗號(,)分隔。
在前端和后端進行數(shù)據(jù)傳輸?shù)臅r候這種格式也是很受歡迎的,后端返回json格式的字符串,前臺使用js中的JSON.parse()方法把JSON字符串解析為json對象,然后進行遍歷,供前端使用。
下面進入正題,介紹在JAVA中JSON和java對象之間的互轉(zhuǎn)。
要想實現(xiàn)JSON和java對象之間的互轉(zhuǎn),需要借助第三方jar包,這里使用json-lib這個jar包,下載地址為:https://sourceforge.net/projects/json-lib/,json-lib需要commons-beanutils-1.8.0.jar、commons-collections-3.2.1.jar、commons-lang-2.5.jar、commons-logging-1.1.1.jar、ezmorph-1.0.6.jar五個包的支持,可以自行從網(wǎng)上下載,這里不再貼出下載地址。
json-lib提供了幾個類可以完成此功能,例,JSONObject、JSONArray。從類的名字上可以看出JSONObject轉(zhuǎn)化的應該是對象格式的,而JSONArray轉(zhuǎn)化的則應該是數(shù)組對象(即,帶[]形式)的。
一、java普通對象和json字符串的互轉(zhuǎn)
java對象--》》字符串
java普通對象指的是java中的一個java bean,即一個實體類,如,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package com.cn.study.day3; 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 + "]" ; } } |
上面是我的一個普通的java實體類,看json-lib如何把它轉(zhuǎn)化為字符串形式,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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字符串,下面看打印的結果,
strJson:{"address":"北京市西城區(qū)","age":"23","name":"JSON"}
strArray:[{"address":"北京市西城區(qū)","age":"23","name":"JSON"}]
從結果中可以看出兩種方法都可以把java對象轉(zhuǎn)化為JSON字符串,只是轉(zhuǎn)化后的結構不同。
JSON字符串--》》java對象
上面說明了如何把java對象轉(zhuǎn)化為JSON字符串,下面看如何把JSON字符串格式轉(zhuǎn)化為java對象,
首先需要定義兩種不同格式的字符串,需要使用\對雙引號進行轉(zhuǎn)義,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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); } |
打印結果為:
stu:Student [name=JSON, age=24, address=北京市西城區(qū)]
stu2:Student [name=JSON, age=24, address=北京市西城區(qū)]
從上面的代碼中可以看出,使用JSONObject可以輕松的把JSON格式的字符串轉(zhuǎn)化為java對象,但是使用JSONArray就沒那么容易了,因為它有“[]”符號,所以我們這里在獲得了JSONArray的對象之后,取其第一個元素即我們需要的一個student的變形,然后使用JSONObject輕松獲得。
二、list和json字符串的互轉(zhuǎn)
list--》》json字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public static void listToJSON(){ Student stu= new Student(); stu.setName( "JSON" ); stu.setAge( "23" ); stu.setAddress( "北京市海淀區(qū)" ); List<Student> lists= new ArrayList<Student>(); lists.add(stu); //1、使用JSONObject //JSONObject listObject=JSONObject.fromObject(lists); //2、使用JSONArray JSONArray listArray=JSONArray.fromObject(lists); //System.out.println("listObject:"+listObject.toString()); System.out.println( "listArray:" +listArray.toString()); } |
我把使用JSONObject的方式給注掉了,我們先看注釋之前的結果,
Exception in thread "main" net.sf.json.JSONException: 'object' is an array. Use JSONArray instead
告訴我說有一個異常,通過查看源碼發(fā)現(xiàn),在使用fromObject方法的時候會先進行參數(shù)類型的判斷,這里就告訴我們,傳入的參數(shù)是一個array類型,因為使用的ArrayList,再來看,注釋之后的結果,
listArray:[{"address":"北京市海淀區(qū)","age":"23","name":"JSON"}]
這樣結果是正常的。
json字符串--》》list
從上面的例子可以看出list的對象只能轉(zhuǎn)化為數(shù)組對象的格式,那么我們看下面的字符串到list的轉(zhuǎn)化,
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public static void jsonToList(){ String arrayStr= "[{\"name\":\"JSON\",\"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); } } |
打印結果,
Student [name=JSON, age=24, address=北京市西城區(qū)]
Student [name=JSON, age=24, address=北京市西城區(qū)]
由于字符串的格式為帶有“[]”的格式,所以這里選擇JSONArray這個對象,它有toArray、toList方法可供使用,前者轉(zhuǎn)化為java中的數(shù)組,或者轉(zhuǎn)化為java中的list,由于這里有實體類進行對應,所以在使用時指定了泛型的類型(Student.class),這樣就可以得到轉(zhuǎn)化后的對象。
三、map和json字符串的互轉(zhuǎn)
map--》》json字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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()); } |
打印結果,
mapObject{"first":{"address":"中國上海","age":"23","name":"JSON"}}
mapArray:[{"first":{"address":"中國上海","age":"23","name":"JSON"}}]
上面打印了兩種形式。
json字符串--》》map
JSON字符串不能直接轉(zhuǎn)化為map對象,要想取得map中的鍵對應的值需要別的方式,
1
2
3
4
5
6
7
8
9
10
|
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()); } |
打印結果,
Student [name=JSON, age=23, address=中國上海]
下面是MyBean的代碼,
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.cn.study.day4; import java.util.Map; import com.cn.study.day3.Student; public class MyBean { private Student first; public Student getFirst() { return first; } public void setFirst(Student first) { this .first = first; } } |
使用toBean()方法是傳入了三個參數(shù),第一個是JSONObject對象,第二個是MyBean.class,第三個是一個Map對象。通過MyBean可以知道此類中要有一個first的屬性,且其類型為Student,要和map中的鍵和值類型對應,即,first對應鍵 first類型對應值的類型。
以上所述是小編給大家介紹的JSON字符串與java對象的相互轉(zhuǎn)換實例詳解,希望對大家有所幫助!
原文鏈接:http://www.cnblogs.com/teach/archive/2016/08/20/5791029.html