jsonobject 與 jsonarray區別
jsonobject:
1
2
3
4
5
|
{ "area" : "武漢" , "name" : "張三" , "age" : 25 } |
jsonarray:
1
2
3
4
5
6
7
8
9
10
|
[{ “area”: “武漢”, “name”: “張三”, “age”: 25 }, { “area”: “深圳”, “name”: “李四”, “age”: 22 }] |
通俗來講 jsonobject 是對象的json形式 jsonarry 是對象集合的json形式。
json 與javabean互轉
json用阿里的fastjson 包
用例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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
public class user { protected long id; protected string account; protected string password; protected string name; protected boolean gender; protected string telephone; @override public string tostring() { return "user{" + "id=" + id + ", account='" + account + '\ '' + ", password='" + password + '\ '' + ", name='" + name + '\ '' + ", gender=" + gender + ", telephone='" + telephone + '\ '' + '}' ; } public boolean isgender() { return gender; } public void setgender( boolean gender) { this .gender = gender; } public string gettelephone() { return telephone; } public void settelephone(string telephone) { this .telephone = telephone; } public string getname() { return name; } public void setname(string name) { this .name = name; } public long getid() { return id; } public void setid( long id) { this .id = id; } public string getaccount() { return account; } public void setaccount(string account) { this .account = account; } public string getpassword() { return password; } public void setpassword(string password) { this .password = password; } } |
1、javabean轉json
方法一:通過java對象轉成string再轉成jsonobject
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.handoop.gms.utils; import com.alibaba.fastjson.jsonobject; import com.handoop.gms.domain.user; public class testmain { public static void main(string []args){ //先通過構造函數初始化一個對象 user user= new user(( long ) 1 , "admin" , "admin" , "張三" , true , "123456" ); //先將java對象轉為string類型 string jsonstring= jsonobject.tojsonstring(user); //再將string類型轉為jsonobject jsonobject jsonobject=jsonobject.parseobject(jsonstring); system.out.println(jsonobject); //轉為jsonobject后就可以隨時根據鍵值獲取他的元素了 system.out.println(jsonobject.get( "password" )); } } |
運行結果
方法2:java對象直接轉json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.handoop.gms.utils; import com.alibaba.fastjson.jsonarray; import com.alibaba.fastjson.jsonobject; import com.handoop.gms.domain.user; public class testmain { public static void main(string []args){ //先通過構造函數初始化一個對象 user user= new user(( long ) 1 , "admin" , "admin" , "張三" , true , "123456" ); jsonobject jsonobject= (jsonobject) jsonobject.tojson(user); system.out.println(jsonobject); } } |
運行結果
json字符串轉jsonobeject
1
2
3
4
5
6
7
|
public class testmain { public static void main(string []args){ string str= "{\"password\":\"admin\",\"gender\":true,\"name\":\"張三\",\"telephone\":\"123456\",\"id\":1,\"account\":\"admin\"}" ; jsonobject jsonobject=jsonobject.parseobject(str); system.out.println( "account: " +jsonobject.get( "account" )+ "---" + "paasword: " +jsonobject.get( "password" )); } } |
運行結果
3.jsonstring 轉jsonarray
1
2
3
4
5
6
7
8
9
10
11
|
public class testmain { public static void main(string []args){ string str= "{\"data\":[{\"password\":\"admin\",\"gender\":true,\"name\":\"張三\",\"telephone\":\"123456\",\"id\":1,\"account\":\"admin\"}]}" ; //先轉成jsonobject jsonobject jsonobject=jsonobject.parseobject(str); //再將jsonobject中數組類型數據取出轉成jsonarray jsonarray jsonarray=jsonobject.getjsonarray( "data" ); system.out.println(jsonarray.get( 0 )); } } |
運行結果
4.json字符串轉java對象
1
2
3
4
|
string str= "{\"password\":\"admin\",\"gender\":true,\"name\":\"張三\",\"telephone\":\"123456\",\"id\":1,\"account\":\"admin\"}" ; // 前面是json字符串 后面是java對象類型 user user=jsonobject.parseobject(str,user. class ); system.out.println( "account: " +user.getaccount()+ "---" + "paasword: " +user.getpassword()); |
輸出結果
到此這篇關于常用json與javabean互轉的方法實現的文章就介紹到這了,更多相關json與javabean互轉內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/zyz_YZJ/article/details/115443741