一、java普通對象和json字符串的互轉
java對象---->json
首先創建一個java對象:
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
|
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形式:
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( "北京市西城區" ); //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兩種方式轉化為json字符串,下面看打印的結果:
json-->javabean
上面說明了如何把java對象轉化為json字符串,下面看如何把json字符串格式轉化為java對象,
首先需要定義兩種不同格式的字符串,需要使用\對雙引號進行轉義。
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\":\"北京市西城區\"}" ; string arraystr= "[{\"name\":\"json\",\"age\":\"24\",\"address\":\"北京市西城區\"}]" ; //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); } |
運行結果:
從上面的代碼中可以看出,使用jsonobject可以輕松的把json格式的字符串轉化為java對象,但是使用jsonarray就沒那么容易了,因為它有“[]”符號,所以我們這里在獲得了jsonarray的對象之后,取其第一個元素即我們需要的一個student的變形,然后使用jsonobject輕松獲得。
二、list和json字符串的互轉
下面將list轉化為json字符串:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public static void convertlistobject() { student stu= new student(); stu.setname( "json" ); stu.setage( "23" ); stu.setaddress( "北京市西城區" ); student stu2= new student(); stu2.setname( "json2" ); stu2.setage( "23" ); stu2.setaddress( "北京市西城區" ); //注意如果是list多個對象比需要使用jsonarray jsonarray array=jsonarray.fromobject(stu); string strarray=array.tostring(); system.out.println( "strarray:" +strarray); } |
運行結果為:
strarray:[{"address":"北京市西城區","name":"json","age":"23"},{"address":"北京市西城區","name":"json2","age":"23"}]
如果使用jsonobject進行轉換會出現:
exception in thread "main" net.sf.json.jsonexception: 'object' is an array. use jsonarray instead
下面將json串轉換為list
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public static void jsontolist(){ string arraystr= "[{\"name\":\"json\",\"age\":\"24\",\"address\":\"北京市西城區\"},{\"name\":\"json2\",\"age\":\"24\",\"address\":\"北京市西城區\"}]" ; //轉化為list list<student> list2=(list<student>)jsonarray.tolist(jsonarray.fromobject(arraystr), student. class ); for (student stu : list2) { system.out.println(stu); } //轉化為數組 student[] ss =(student[])jsonarray.toarray(jsonarray.fromobject(arraystr),student. class ); for (student student : ss) { system.out.println(student); } } |
運行結果為:
三、map和json字符串的互轉
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()); } |
運行結果:
json字符串轉化為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方法,需要三個參數 mybean my=(mybean)jsonobject.tobean(jsonobject, mybean. class , map); system.out.println(my.getfirst()); } |
注意在轉化為map的時候需要創建一個類,類里面需要有first屬性跟json串里面的一樣:
使用tobean()
方法是傳入了三個參數,第一個是jsonobject對象,第二個是mybean.class,第三個是一個map對象。通過mybean可以知道此類中要有一個first的屬性,且其類型為student,要和map中的鍵和值類型對應,即,first對應鍵 first類型對應值的類型。
運行結果:
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/u014320421/article/details/84315000