使用Jackson解析Json示例
原文http://blog.csdn.net/gebitan505/article/details/17005735
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及解析:

import java.io.IOException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Bean{
private String country;
private boolean orgood;
private Country countryInfo;
private CountryDetail[] countryDetail;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public boolean isOrgood() {
return orgood;
}
public void setOrgood(boolean orgood) {
this.orgood = orgood;
}
public Country getCountryInfo() {
return countryInfo;
}
public void setCountryInfo(Country countryInfo) {
this.countryInfo = countryInfo;
}
public CountryDetail[] getCountryDetail() {
return countryDetail;
}
public void setCountryDetail(CountryDetail[] countryDetail) {
this.countryDetail = countryDetail;
}
public static class Country{
public String getGolden() {
return golden;
}
public void setGolden(String golden) {
this.golden = golden;
}
public String getSilver() {
return silver;
}
public void setSilver(String silver) {
this.silver = silver;
}
public String getCopper() {
return copper;
}
public void setCopper(String copper) {
this.copper = copper;
}
public String getTank() {
return tank;
}
public void setTank(String tank) {
this.tank = tank;
}
private String golden;
private String silver;
private String copper;
private String tank;
}
public static class CountryDetail{
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getHeader() {
return header;
}
public void setHeader(String header) {
this.header = header;
}
public String getFieldIndex() {
return fieldIndex;
}
public void setFieldIndex(String fieldIndex) {
this.fieldIndex = fieldIndex;
}
public String getSortOrder() {
return sortOrder;
}
public void setSortOrder(String sortOrder) {
this.sortOrder = sortOrder;
}
public boolean isPrintable() {
return printable;
}
public void setPrintable(boolean printable) {
this.printable = printable;
}
private String id;
private String header;
private String fieldIndex;
private String sortOrder;
private boolean printable;
}
public static void main(String args[]){
ObjectMapper mapper = new ObjectMapper();
try {
Bean bean = mapper.readValue(new File("custom.json"), Bean.class);
//读取
System.out.println("Country:"+bean.getCountry());
System.out.println("CountryInfo:"+bean.getCountryInfo().getGolden());
System.out.println("CountryDetail:"+bean.getCountryDetail()[1].getHeader());
System.out.println("orgood:"+bean.isOrgood());
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
使用Jackson解析Json示例的更多相关文章
- JackSon解析json字符串
JackSon解析json字符串 原文:http://blog.csdn.net/java_huashan/article/details/9353903 概述 jackson解析json例子 准备工 ...
- 记一次FastJSON和Jackson解析json时遇到的中括号问题
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jadyer/article/details/24395015 完整版见https://jadyer. ...
- 使用 jackson 解析 json 演示样例
首先须要下载3个包,下载地址在Github FasterXML,这三个核心模块各自是: Streaming ("jackson-core") defines low-level s ...
- Jackson 解析json数据之忽略解析字段注解@JsonIgnoreProperties
转自:http://blog.csdn.net/ngl272/article/details/70217104 以前解析json用的惯的就是Google的gson了,用惯了基本就用它了,一直也没发现什 ...
- [转]Jackson 解析json数据之忽略解析字段注解@JsonIgnoreProperties
以前解析json用的惯的就是Google的gson了,用惯了基本就用它了,一直也没发现什么大问题,因为都是解析简单的json数据.但是最近学习springboot,要解析一个比较复杂的json数据.就 ...
- Jackson 解析 JSON 详细教程
点赞再看,动力无限. 微信搜「程序猿阿朗 」. 本文 Github.com/niumoo/JavaNotes 和 未读代码博客 已经收录,有很多知识点和系列文章. JSON 对于开发者并不陌生,如今的 ...
- 使用jackson解析JSON数据
本文介绍使用jackson来对json数据进行解析操作 首先,需要去官网下载jackson,本文使用的是(jackson-all-1.9.11.jar) 主要通过ObjectMapper对json进行 ...
- 使用jackson解析json串得到树模型,然后遍历树模型获得需要的数据
Problem:从网址 http://quotes.money.163.com/hs/service/marketradar_ajax.php?host=http%3A%2F%2Fquotes.mon ...
- jackson 解析json问题
1.json串中有key为A,但指定转换的mybean中未定义属性A,会抛异常.处理:mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, fals ...
随机推荐
- Nancy之基于Self Hosting的补充小Demo
前面把Hosting Nancy with ASP.NET.Self Hosting Nancy和Hosting Nancy with OWIN 以demo的形式简单描述了一下. 这篇是为Self H ...
- List集合去重的一种方法
前一段时间们需要对一个List<Model>集合去重,情况是该集合中会出现多个Name属性值相同的,但是其他属性值不同的数据. 在这种情况下,需求要只保留其中一个就好. 我觉得遍历和Has ...
- php中的M方法
- oracle函数大全(转载)
F.1字符函数--返回字符值 这些函数全都接收的是字符族类型的参数(CHR除外)并且返回字符值.除了特别说明的之外,这些函数大部分返回VARCHAR2类型的数值.字符函数的返回类型所受的限制和基本数据 ...
- 如何在MFC界面开发中响应Button按钮的Down和Up事件
通过尝试有两种方案可以解决这个问题,第一种方案是通过PreTranslateMessage函数在调度消息之前对消息类型进行筛选,第二种方案是重载CButton类,在重载后的类CForTestButto ...
- Lind.DDD.Caching分布式数据集缓存介绍
回到目录 戏说当年 大叔原创的分布式数据集缓存在之前的企业级框架里介绍过,大家可以关注<我心中的核心组件(可插拔的AOP)~第二回 缓存拦截器>,而今天主要对Lind.DDD.Cachin ...
- HTML、CSS、JavaScript和PHP的注释
- Javascript中addEventListener和attachEvent的区别
在利用javascript为DOM Element添加事件处理程序时,如果要想下兼容IE6,7时,就不得不考虑addEventListener与attachEvent的异同. 1.首先说下addEve ...
- js正则表达式图形化工具-rline
github地址:https://github.com/finance-sh/rline 在线demo: http://lihuazhai.com/demo/test.html 这是一个js正则表达式 ...
- Semaphore(计数信号量)
//对象池public class Pool<T> { private int size; private List<T> items = new ArrayList<T ...