首页 > 代码库 > jackson简单使用案例
jackson简单使用案例
准备:下载jackson的jar包,不要使用2.8版本,类加载器错误,原因未知
entity: getter和setter省略
public class Person {
private int id;
private String name;
@JsonProperty(value="http://www.mamicode.com/ismale")
private boolean gender;
@JsonFormat(pattern="yyyy-MM-dd") //输入的格式必须满足,不然抛异常;json串也为该格式
private Date birthday;
@JsonIgnore
private List<String> hobby;
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", gender=" + gender
+ ", birthday=" + birthday + ", hobby=" + hobby + "]";
}
public Person() {
super();
}
}
//demo
package jackson;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import entity.Person;
public class Demo {
/**
* json序列化
*
* 1,javabean转化为json
* 2,map转化为json
* 3,对象集合转化为json
* 4,复杂对象转换为json
*/
@Test
public void beanToJson() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
/**
* bean换成json
*/
Person p = new Person(1, "略略", false, new Date(), Arrays.asList(new String[] {"打球", "游戏", "动漫"}));
String json = mapper.writeValueAsString(p);
System.out.println(json);
/**
* map换成json
*/
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 110);
map.put("name", 3);
map.put("birthday", "2098-11-23");
json = mapper.writeValueAsString(map);
System.out.println(json);
/**
* beanList换成json
*/
Person[] ps = { new Person(1, "略略", false, new Date(), Arrays.asList(new String[] {"打球", "游戏", "动漫"})),
new Person(2, "哈哈", false, new Date(), Arrays.asList(new String[] {"打球"}))
};
json = mapper.writeValueAsString(ps);
System.out.println(json);
}
/**
* jackson注解 在entiry中使用
* 1,序列化时忽略属性@JsonIgnore
* 2,格式化日期属性@JsonFormat
* 3,把属性名序列化成另一个属性名@JsonProperty
*/
/**
* json反序列化
*
* 1,json转化Array
* 2,json转为javabean
* 3,json转为map
* 4,jason转为beanList
* 5,json转为List<Map<>>
* @throws IOException
* @throws JsonMappingException
* @throws JsonParseException
*
*/
@Test
public void jsonToBean() throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
/**
* json转为javabean
*/
String str = "{\"id\":1,\"name\":\"略略\",\"birthday\":\"2017-04-14\",\"ismale\":false}";
Person p = mapper.readValue(str, Person.class);
System.out.println(p);
/**
* json转为map
*/
Map<String, Object> map = mapper.readValue(str, Map.class);
System.out.println(map);
str = "[{\"id\":1,\"name\":\"略略\",\"birthday\":\"2017-04-14\",\"ismale\":false},{\"id\":2,\"name\":\"哈哈\",\"birthday\":\"2017-04-14\",\"ismale\":false}]";
/**
* json转换成List<LinkedHashMap<K, V>>集合
*
* 注意:
* 这里可与i转换成List<Object> 但是不能转换成具体的类
* 解决:
* 可以先转化为Person[] 数组,然后再转化为List<Person>
*/
// List<Object> ps = mapper.readValue(str, List.class);
// for (Object obj : ps) {
// System.out.println(obj);
// }
List<Map<String, Object>> list = mapper.readValue(str, List.class);
for (Map<String, Object> ms : list) {
System.out.println(ms);
Set<String> keySet = ms.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
System.out.println(key + "--" + ms.get(key));
}
}
Person[] ps = mapper.readValue(str, Person[].class);
for (Person person : ps) {
System.out.println(person.toString());
}
}
/**
* json与xml的相互转换
*/
}
jackson简单使用案例