Java--Json解析

普通Json

1 {"code":"S0000", "describe":"数据正常返回", "result":"OK"}

 1 package gsondemo;
 2 
 3 import java.io.Serializable;
 4 import java.util.List;
 5 
 6 @SuppressWarnings("serial")
 7 public class Common implements Serializable {
 8 
 9     private String code;
10     private String describe;
11     private String result;
12 
13     public String getCode() {
14         return code;
15     }
16 
17     public void setCode(String code) {
18         this.code = code;
19     }
20 
21     public String getDescribe() {
22         return describe;
23     }
24 
25     public void setDescribe(String describe) {
26         this.describe = describe;
27     }
28 
29     public String getResult() {
30         return result;
31     }
32 
33     public void setResult(String result) {
34         this.result = result;
35     }
36 
37     @Override
38     public String toString() {
39         return "Common [code=" + code + ", describe=" + describe + ", result=" + result + "]";
40     }
41 
42 }
 1 package gsondemo;
 2 
 3 import com.google.gson.Gson;
 4 
 5 public class CommonAna {
 6     
 7     public static void main(String[] args) {
 8         String json = "{\"code\":\"S0000\", \"describe\":\"数据正常返回\", \"result\":\"OK\"}";
 9         Gson gson = new Gson();
10         Common common = gson.fromJson(json, Common.class);
11         System.out.println(common);
12     }
13 
14 }

 

嵌套json对象

 1 {"code":"S0000",
 2 "describe":"数据正常返回",
 3 "result":{"accountBlank":"xxxxxx",
 4     "bankAccount":"xxxxxx",
 5     "code":"xxx",
 6     "id":"xxxxxxx",
 7     "kpAddr":"xxxxxx",
 8     "kpCode":"xxxxx",
 9     "kpName":"xxxxx",
10     "kpTel":"xxx"}}
 1 package gsondemo;
 2 
 3 import java.io.Serializable;
 4 
 5 @SuppressWarnings("serial")
 6 public class Sub implements Serializable {
 7 
 8     private String accountBlank;
 9     private String bankAccount;
10     private String code;
11     private String id;
12     private String kpAddr;
13     private String kpCode;
14     private String kpName;
15     private String kpTel;
16 
17     public String getAccountBlank() {
18         return accountBlank;
19     }
20 
21     public void setAccountBlank(String accountBlank) {
22         this.accountBlank = accountBlank;
23     }
24 
25     public String getBankAccount() {
26         return bankAccount;
27     }
28 
29     public void setBankAccount(String bankAccount) {
30         this.bankAccount = bankAccount;
31     }
32 
33     public String getCode() {
34         return code;
35     }
36 
37     public void setCode(String code) {
38         this.code = code;
39     }
40 
41     public String getId() {
42         return id;
43     }
44 
45     public void setId(String id) {
46         this.id = id;
47     }
48 
49     public String getKpAddr() {
50         return kpAddr;
51     }
52 
53     public void setKpAddr(String kpAddr) {
54         this.kpAddr = kpAddr;
55     }
56 
57     public String getKpCode() {
58         return kpCode;
59     }
60 
61     public void setKpCode(String kpCode) {
62         this.kpCode = kpCode;
63     }
64 
65     public String getKpName() {
66         return kpName;
67     }
68 
69     public void setKpName(String kpName) {
70         this.kpName = kpName;
71     }
72 
73     public String getKpTel() {
74         return kpTel;
75     }
76 
77     public void setKpTel(String kpTel) {
78         this.kpTel = kpTel;
79     }
80 
81     @Override
82     public String toString() {
83         return "NuonuoBean_Result [accountBlank=" + accountBlank + ", bankAccount=" + bankAccount + ", code=" + code
84                 + ", id=" + id + ", kpAddr=" + kpAddr + ", kpCode=" + kpCode + ", kpName=" + kpName + ", kpTel=" + kpTel
85                 + "]";
86     }
87 
88 }
 1 package gsondemo;
 2 
 3 import java.io.Serializable;
 4 import java.util.List;
 5 
 6 @SuppressWarnings("serial")
 7 public class CommonWithObject implements Serializable {
 8 
 9     private String code;
10     private String describe;
11     private Sub result;
12 
13     public String getCode() {
14         return code;
15     }
16 
17     public void setCode(String code) {
18         this.code = code;
19     }
20 
21     public String getDescribe() {
22         return describe;
23     }
24 
25     public void setDescribe(String describe) {
26         this.describe = describe;
27     }
28 
29     public Sub getResult() {
30         return result;
31     }
32 
33     public void setResult(Sub result) {
34         this.result = result;
35     }
36 
37     @Override
38     public String toString() {
39         return "CommonWithObject [code=" + code + ", describe=" + describe + ", result=" + result + "]";
40     }
41 
42 }
 1 package gsondemo;
 2 
 3 import com.google.gson.Gson;
 4 
 5 public class WithObjectAna {
 6     
 7     public static void main(String[] args) {
 8         String json =   "{\"code\":\"S0000\","
 9                 +"            \"describe\":\"数据正常返回\","
10                 +"            \"result\":{\"accountBlank\":\"xxxxxx\","
11                 +"                         \"bankAccount\":\"xxxxxx\","
12                 +"                            \"code\":\"xxx\","
13                 +"                          \"id\":\"xxxxxxx\","
14                 +"                          \"kpAddr\":\"xxxxxx\","
15                 +"                          \"kpCode\":\"xxxxx\","
16                 +"                          \"kpName\":\"xxxxx\","
17                 +"                           \"kpTel\":\"xxx\"}}";
18             Gson gson = new Gson();
19             CommonWithObject commonWithObject = gson.fromJson(json, CommonWithObject.class);
20             System.out.println(commonWithObject);
21     }
22 
23 }

 

 

嵌套json数组

 1 {"code":"S0000",
 2 "describe":"数据正常返回",
 3 "result":[{"accountBlank":"xxxxxx",
 4     "bankAccount":"xxxxxx",
 5     "code":"xxx",
 6     "id":"xxxxxxx",
 7     "kpAddr":"xxxxxx",
 8     "kpCode":"xxxxx",
 9     "kpName":"xxxxx",
10     "kpTel":"xxx"}]}
 1 package gsondemo;
 2 
 3 import java.io.Serializable;
 4 import java.util.List;
 5 
 6 @SuppressWarnings("serial")
 7 public class CommonWithArray implements Serializable {
 8     
 9     private String code;
10     private String describe;
11     private List<Sub> result;
12 
13     public String getCode() {
14         return code;
15     }
16 
17     public void setCode(String code) {
18         this.code = code;
19     }
20 
21     public String getDescribe() {
22         return describe;
23     }
24 
25     public void setDescribe(String describe) {
26         this.describe = describe;
27     }
28 
29     public List<Sub> getResult() {
30         return result;
31     }
32 
33     public void setResult(List<Sub> result) {
34         this.result = result;
35     }
36 
37     @Override
38     public String toString() {
39         return "CommonWithObject [code=" + code + ", describe=" + describe + ", result=" + result + "]";
40     }
41 
42 }
 1 package gsondemo;
 2 
 3 import com.google.gson.Gson;
 4 
 5 public class WithArrayAna {
 6     
 7     public static void main(String[] args) {
 8         String json =   "{\"code\":\"S0000\","
 9                 +"            \"describe\":\"数据正常返回\","
10                 +"            \"result\":[{\"accountBlank\":\"xxxxxx\","
11                 +"                         \"bankAccount\":\"xxxxxx\","
12                 +"                            \"code\":\"xxx\","
13                 +"                          \"id\":\"xxxxxxx\","
14                 +"                          \"kpAddr\":\"xxxxxx\","
15                 +"                          \"kpCode\":\"xxxxx\","
16                 +"                          \"kpName\":\"xxxxx\","
17                 +"                           \"kpTel\":\"xxx\"}]}";
18             Gson gson = new Gson();
19             CommonWithArray commonWithArray = gson.fromJson(json, CommonWithArray.class);
20             System.out.println(commonWithArray);
21     }
22 
23 }

 

主要区别就是bean类的区别,根据解析的情况组合所需要的bean

posted @ 2017-03-27 14:18  MicroCat  阅读(159)  评论(0编辑  收藏  举报