jacksonall的使用,解析json

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jacksonall的使用,解析json相关的知识,希望对你有一定的参考价值。

转自:http://www.cnblogs.com/lee0oo0/archive/2012/08/23/2652751.html ,

Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json、xml转换成Java对象。可以参考:

Jackson 框架,轻易转换JSON

首先需要下载包:jackson-databind-2.0.5.jar jackson-annotations-2.0.5.jar jackson-core-2.0.5.jar,网址在:http://wiki.fasterxml.com/JacksonDownload 

custom.json:

{
    "country":"USA",   
    "countryInfo":{"golden":40,"silver":23,"copper":33,"tank":1},  
"countryDetail":[  
    {"id":"no","header":"号码","fieldIndex":"no","sortOrder":null,"printable":true},  
    {"id":"name","header":"姓名","fieldIndex":"name","sortOrder":null,"printable":true},  
    {"id":"total","header":"总成绩","fieldIndex":"total","sortOrder":null,"printable":true},  
    {"id":"detail","header":"详细信息","fieldIndex":"detail","sortOrder":null,"printable":true}  
   ], 
"orgood":true}  

Java的Bean及解析: 

  1 public class Bean {
  2 
  3     private String country;
  4     private boolean orgood;
  5     private Country countryInfo;
  6     private CountryDetail[] countryDetail;
  7 
  8     public String getCountry() {
  9         return country;
 10     }
 11 
 12     public void setCountry(String country) {
 13         this.country = country;
 14     }
 15 
 16     public boolean isOrgood() {
 17         return orgood;
 18     }
 19 
 20     public void setOrgood(boolean orgood) {
 21         this.orgood = orgood;
 22     }
 23 
 24     public Country getCountryInfo() {
 25         return countryInfo;
 26     }
 27 
 28     public void setCountryInfo(Country countryInfo) {
 29         this.countryInfo = countryInfo;
 30     }
 31 
 32     public CountryDetail[] getCountryDetail() {
 33         return countryDetail;
 34     }
 35 
 36     public void setCountryDetail(CountryDetail[] countryDetail) {
 37         this.countryDetail = countryDetail;
 38     }
 39 
 40     public static class Country {
 41         public String getGolden() {
 42             return golden;
 43         }
 44 
 45         public void setGolden(String golden) {
 46             this.golden = golden;
 47         }
 48 
 49         public String getSilver() {
 50             return silver;
 51         }
 52 
 53         public void setSilver(String silver) {
 54             this.silver = silver;
 55         }
 56 
 57         public String getCopper() {
 58             return copper;
 59         }
 60 
 61         public void setCopper(String copper) {
 62             this.copper = copper;
 63         }
 64 
 65         public String getTank() {
 66             return tank;
 67         }
 68 
 69         public void setTank(String tank) {
 70             this.tank = tank;
 71         }
 72 
 73         private String golden;
 74         private String silver;
 75         private String copper;
 76         private String tank;
 77 
 78     }
 79 
 80     public static class CountryDetail {
 81         public String getId() {
 82             return id;
 83         }
 84 
 85         public void setId(String id) {
 86             this.id = id;
 87         }
 88 
 89         public String getHeader() {
 90             return header;
 91         }
 92 
 93         public void setHeader(String header) {
 94             this.header = header;
 95         }
 96 
 97         public String getFieldIndex() {
 98             return fieldIndex;
 99         }
100 
101         public void setFieldIndex(String fieldIndex) {
102             this.fieldIndex = fieldIndex;
103         }
104 
105         public String getSortOrder() {
106             return sortOrder;
107         }
108 
109         public void setSortOrder(String sortOrder) {
110             this.sortOrder = sortOrder;
111         }
112 
113         public boolean isPrintable() {
114             return printable;
115         }
116 
117         public void setPrintable(boolean printable) {
118             this.printable = printable;
119         }
120 
121         private String id;
122         private String header;
123         private String fieldIndex;
124         private String sortOrder;
125         private boolean printable;
126 
127     }
128 
129     public static void main(String args[]) {
130         ObjectMapper mapper = new ObjectMapper();
131         try {
132             Bean bean = mapper.readValue(new File("custom.json"), Bean.class); // 读取
133                                                                                 // System.out.println("Country:"+bean.getCountry());
134             System.out.println("CountryInfo:" + bean.getCountryInfo().getGolden());
135             System.out.println("CountryDetail:" + bean.getCountryDetail()[1].getHeader());
136             System.out.println("orgood:" + bean.isOrgood());
137         } catch (JsonParseException e) {
138             // TODO Auto-generated catch block
139             e.printStackTrace();
140         } catch (JsonMappingException e) {
141             // TODO Auto-generated catch block
142             e.printStackTrace();
143         } catch (IOException e) {
144             // TODO Auto-generated catch block
145             e.printStackTrace();
146         }
147     }
148 }

 

以上是关于jacksonall的使用,解析json的主要内容,如果未能解决你的问题,请参考以下文章

使用 json rereiver php mysql 在片段中填充列表视图

解析:JSON 文本没有以数组或对象开头,并且允许未设置片段的选项

ios - Heroku 和 MongoDb 上的自定义解析服务器错误 3080:JSON 文本没有以数组或对象开头,并且允许未设置片段的选项

NodeJs GraphQL 片段解析器

Golang实践录:反射reflect的一些研究及代码汇总

不要在片段中显示列表视图项