jackson序列化和反序列化(单个对象、列表)
2015-05-29 14:45
399 查看
[html] view
plaincopy
1.maven配置
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>1.7.4</version>
</dependency>
2.新建一个person对象:
[java] view
plaincopy
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonProperty;
public class Person {
@JsonProperty("n")//这个是标识序列化时用n来代替name
private String name;
private String sex;
@JsonIgnore//这个是标识不对该属性进行序列化
private int age;
@JsonProperty("a")
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
3.整个测试类:
[java] view
plaincopy
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
public class TestJson {
private ObjectMapper objectMapper = new ObjectMapper();
//单个对象的序列化
public void testTojson(){
Person testPerson = new Person();
testPerson.setAddress("dsfsdf");
testPerson.setAge(10);
testPerson.setName("name");
testPerson.setSex(null);
try {
String resultStr = objectMapper.writeValueAsString(testPerson);
System.out.println(resultStr);
} catch (JsonGenerationException 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();
}
}
//将json反序列化为对象
public void testFromJson(){
String content = "{\"n\":\"name\",\"sex\":\"sec\",\"a\":\"null\"}";
try {
Person resultPerson = objectMapper.readValue(content, Person.class);
System.out.println(resultPerson.getAddress().trim());
System.out.println(resultPerson.getAge());
} 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();
}
}
//对象列表的序列化
public void testFromList(){
Person testPerson1 = new Person();
testPerson1.setAddress("dsfsdf111");
testPerson1.setAge(10);
testPerson1.setName("name11");
testPerson1.setSex("sec11");
Person testPerson2 = new Person();
testPerson2.setAddress("dsfsdf222");
testPerson2.setAge(10);
testPerson2.setName("name22");
testPerson2.setSex("sec22");
Person testPerson3 = new Person();
testPerson3.setAddress("dsfsdf333");
testPerson3.setAge(10);
testPerson3.setName("name33");
testPerson3.setSex("sec33");
try {
List<Person> personList = new ArrayList<Person>();
personList.add(testPerson1);
personList.add(testPerson2);
personList.add(testPerson3);
String resultStr = objectMapper.writeValueAsString(personList);
System.out.println(resultStr);
} catch (JsonGenerationException 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();
}
}
//将josn串反序列化为对象列表
public void testListFromJson(){
String content = "[{\"sex\":\"sec11\",\"n\":\"name11\",\"a\":\"dsfsdf111\"}," +
"{\"sex\":\"sec22\",\"n\":\"name22\",\"a\":\"dsfsdf222\"}," +
"{\"sex\":\"sec33\",\"n\":\"name33\",\"a\":\"dsfsdf333\"}]";
try {
//这个有问题
// List<Person> resultPerson = objectMapper.readValue(content, ArrayList.class);
// System.out.println(resultPerson.get(0).getAddress());
List<Person> d = objectMapper.readValue(content, new TypeReference<List<Person>>(){});
System.out.println(d.get(0).getAddress());
} 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();
}
}
public static void main(String[] args) {
TestJson test = new TestJson();
test.testTojson();
test.testFromJson();
test.testFromList();
test.testListFromJson();
}
}
plaincopy
1.maven配置
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>1.7.4</version>
</dependency>
2.新建一个person对象:
[java] view
plaincopy
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonProperty;
public class Person {
@JsonProperty("n")//这个是标识序列化时用n来代替name
private String name;
private String sex;
@JsonIgnore//这个是标识不对该属性进行序列化
private int age;
@JsonProperty("a")
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
3.整个测试类:
[java] view
plaincopy
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
public class TestJson {
private ObjectMapper objectMapper = new ObjectMapper();
//单个对象的序列化
public void testTojson(){
Person testPerson = new Person();
testPerson.setAddress("dsfsdf");
testPerson.setAge(10);
testPerson.setName("name");
testPerson.setSex(null);
try {
String resultStr = objectMapper.writeValueAsString(testPerson);
System.out.println(resultStr);
} catch (JsonGenerationException 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();
}
}
//将json反序列化为对象
public void testFromJson(){
String content = "{\"n\":\"name\",\"sex\":\"sec\",\"a\":\"null\"}";
try {
Person resultPerson = objectMapper.readValue(content, Person.class);
System.out.println(resultPerson.getAddress().trim());
System.out.println(resultPerson.getAge());
} 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();
}
}
//对象列表的序列化
public void testFromList(){
Person testPerson1 = new Person();
testPerson1.setAddress("dsfsdf111");
testPerson1.setAge(10);
testPerson1.setName("name11");
testPerson1.setSex("sec11");
Person testPerson2 = new Person();
testPerson2.setAddress("dsfsdf222");
testPerson2.setAge(10);
testPerson2.setName("name22");
testPerson2.setSex("sec22");
Person testPerson3 = new Person();
testPerson3.setAddress("dsfsdf333");
testPerson3.setAge(10);
testPerson3.setName("name33");
testPerson3.setSex("sec33");
try {
List<Person> personList = new ArrayList<Person>();
personList.add(testPerson1);
personList.add(testPerson2);
personList.add(testPerson3);
String resultStr = objectMapper.writeValueAsString(personList);
System.out.println(resultStr);
} catch (JsonGenerationException 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();
}
}
//将josn串反序列化为对象列表
public void testListFromJson(){
String content = "[{\"sex\":\"sec11\",\"n\":\"name11\",\"a\":\"dsfsdf111\"}," +
"{\"sex\":\"sec22\",\"n\":\"name22\",\"a\":\"dsfsdf222\"}," +
"{\"sex\":\"sec33\",\"n\":\"name33\",\"a\":\"dsfsdf333\"}]";
try {
//这个有问题
// List<Person> resultPerson = objectMapper.readValue(content, ArrayList.class);
// System.out.println(resultPerson.get(0).getAddress());
List<Person> d = objectMapper.readValue(content, new TypeReference<List<Person>>(){});
System.out.println(d.get(0).getAddress());
} 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();
}
}
public static void main(String[] args) {
TestJson test = new TestJson();
test.testTojson();
test.testFromJson();
test.testFromList();
test.testListFromJson();
}
}
相关文章推荐
- C编写Windows服务程序学习
- struts2中的constant配置详解
- 矩阵中求子矩形
- if { ...... } else { ...... }
- linux学习笔记-特殊文件
- MyBatis注解Annotation介绍及Demo
- 使用ant实现自动化示例
- overthewire朝花夕拾
- Android scrollview嵌套listview时自动滑动问题
- java基础整理——UDP和TCP通讯协议小结
- int fun()const{}结构解析记录
- Android运行时异常“Binary XML file line # : Error inflating class”
- EHCACHE+spring+springmvc+maven 使用及场景
- 线性表学习归纳总结二:线性表顺序存储实现
- Web_JavaScript_去除JS字串两边空格;
- 网上找的查询本地接口的代码 来记录一下
- 2015年任务
- eclipse编辑XML的时候启动提示功能(图文)
- OSError: [Errno 2] No such file or directory
- HashMap与HashTable的区别、HashMap与HashSet的关系