jacksonall的使用,解析json
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了jacksonall的使用,解析json,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3850字,纯文字阅读大概需要6分钟。
内容图文
![jacksonall的使用,解析json](/upload/InfoBanner/zyjiaocheng/1045/09807cf9ffe64e1da3557c2d41f15580.jpg)
转自: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 16publicboolean isOrgood() { 17return orgood; 18 } 19 20publicvoid setOrgood(boolean orgood) { 21this.orgood = orgood; 22 } 23 24public Country getCountryInfo() { 25return countryInfo; 26 } 27 28publicvoid setCountryInfo(Country countryInfo) { 29this.countryInfo = countryInfo; 30 } 31 32public CountryDetail[] getCountryDetail() { 33return countryDetail; 34 } 35 36publicvoid setCountryDetail(CountryDetail[] countryDetail) { 37this.countryDetail = countryDetail; 38 } 39 40publicstaticclass Country { 41public String getGolden() { 42return golden; 43 } 44 45publicvoid setGolden(String golden) { 46this.golden = golden; 47 } 48 49public String getSilver() { 50return silver; 51 } 52 53publicvoid setSilver(String silver) { 54this.silver = silver; 55 } 56 57public String getCopper() { 58return copper; 59 } 60 61publicvoid setCopper(String copper) { 62this.copper = copper; 63 } 64 65public String getTank() { 66return tank; 67 } 68 69publicvoid setTank(String tank) { 70this.tank = tank; 71 } 72 73private String golden; 74private String silver; 75private String copper; 76private String tank; 77 78 } 79 80publicstaticclass CountryDetail { 81public String getId() { 82return id; 83 } 84 85publicvoid setId(String id) { 86this.id = id; 87 } 88 89public String getHeader() { 90return header; 91 } 92 93publicvoid setHeader(String header) { 94this.header = header; 95 } 96 97public String getFieldIndex() { 98return fieldIndex; 99 } 100101publicvoid setFieldIndex(String fieldIndex) { 102this.fieldIndex = fieldIndex; 103 } 104105public String getSortOrder() { 106return sortOrder; 107 } 108109publicvoid setSortOrder(String sortOrder) { 110this.sortOrder = sortOrder; 111 } 112113publicboolean isPrintable() { 114return printable; 115 } 116117publicvoid setPrintable(boolean printable) { 118this.printable = printable; 119 } 120121private String id; 122private String header; 123private String fieldIndex; 124private String sortOrder; 125privateboolean printable; 126127 } 128129publicstaticvoid main(String args[]) { 130 ObjectMapper mapper = new ObjectMapper(); 131try { 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 block139 e.printStackTrace(); 140 } catch (JsonMappingException e) { 141// TODO Auto-generated catch block142 e.printStackTrace(); 143 } catch (IOException e) { 144// TODO Auto-generated catch block145 e.printStackTrace(); 146 } 147 } 148 }
原文:http://www.cnblogs.com/Sunnor/p/5173065.html
内容总结
以上是互联网集市为您收集整理的jacksonall的使用,解析json全部内容,希望文章能够帮你解决jacksonall的使用,解析json所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。