Json是一种高效的数据结构,在Android与服务器后台交互的过程中常常涉及到Json数据的封装,在这篇文章中将会分析如何讲json数据反序列化为对象,进行进一步的数据封装。
首先json的数据格式先做一个简单的介绍,如下格式
{
"response": [
{
"name": "小周",
"age": 20,
"birthday": 844099200000,
"email": "xiaomin@sina.com",
"countryInfo": {
"golden": 40,
"silver": 23,
"copper": 33,
"tank": 1
},
"countryDetail": [
{
"id": "no",
"header": "号码",
"printable": true
},
{
"id": "name",
"header": "姓名",
"printable": true
},
{
"id": "detail",
"header": "详细",
"printable": false
}
]
}
]
}
这是一个标准的从服务器传回的Json数据,我自己对于json的理解是“[]”中封装的是数组数据,"{}"中封装的是对象数据,而key-value的形式是属性和值的对应关系。也就是说再创建对应的JavaBean的时候见到"[]"应当将其用数组或者list,set等容器封装起来,而"{}"中的数据应当当成一个对象封装起来
封装起来的效果如下代码:
public class UserJson {
private List<User> response;
public List<User> getResponse() {
return response;
}
public void setResponse(List<User> response) {
this.response = response;
}
}
<pre name="code" class="java">private String name;
private Integer age;
private Date birthday;
private String email;
private Country countryInfo;
private List<CountryDetail> countryDetail;
public List<CountryDetail> getCountryDetail() {
return countryDetail;
}
public void setCountryDetail(List<CountryDetail> countryDetail) {
this.countryDetail = countryDetail;
}
public Country getCountryInfo() {
return countryInfo;
}
public void setCountryInfo(Country countryInfo) {
this.countryInfo = countryInfo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
<pre name="code" class="java">public class Country {
private int golden;
private int silver;
private int copper;
private int tank;
public int getGolden() {
return golden;
}
public void setGolden(int golden) {
this.golden = golden;
}
public int getSilver() {
return silver;
}
public void setSilver(int silver) {
this.silver = silver;
}
public int getCopper() {
return copper;
}
public void setCopper(int copper) {
this.copper = copper;
}
public int getTank() {
return tank;
}
public void setTank(int tank) {
this.tank = tank;
}
}
<pre name="code" class="java">public class CountryDetail {
private String id;
private String header;
private boolean printable;
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 boolean isPrintable() {
return printable;
}
public void setPrintable(boolean printable) {
this.printable = printable;
}
}
上面是有关javabean 的封装,接下来是利用Jsason的反序列化,处理数据
public class JsonParse2 {
final static String json = "{ \"response\":[ {\"name\":\"小民\",\"age\":20,\"birthday\":844099200000,\"email\":\"xiaomin@sina.com\""
+ ",\"countryInfo\":{\"golden\": 40,\"silver\": 23,\"copper\": 33,\"tank\": 1},\"countryDetail\":[{\"id\":\"no\","
+ "\"header\":\"号码\",\"printable\":true},{\"id\":\"name\",\"header\":\"姓名\",\"printable\":true},{\"id\":\"detail\",\"header\":\"详细\",\"printable\":false}] }]}";
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
User user=null;
try {
UserJson userJson = mapper.readValue(json,
new TypeReference<UserJson>() {
});
user = userJson.getResponse().get(0);
// System.out.println(userJson.getResponse().getCountryDetail().get(0)
// .getHeader());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(user.getCountryDetail().get(0).getHeader());
}
}
需要注意的是TypeReference的使用,因为jackson把数据默认解析成了Map对象,我们需要用泛型指明其目标类型。这只是一个简单的例子,帮助大家理解,本人水平有限,若有纰漏之处,希望指正,转载请注明出处