package commonsTest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import models.Person;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.junit.Test;
/**
* jackson实现json与对象,集合,map之间的转换
*/
public class Demo3 {
/**
* 将map转换成json字符串
* {"age":23,"name":"张三","addr":"北京"}
* @throws Exception
*/
@Test
public void test1() throws Exception{
Map<String,Object> map = new HashMap<String,Object>();
map.put("name", "张三");
map.put("age", 23);
map.put("addr", "北京");
ObjectMapper mapper = new ObjectMapper();
String string = mapper.writeValueAsString(map);
System.out.println(string);
}
/**
* json字符串转换成map对象
* {name=张三, age=23, addr=北京}
* @throws Exception
*/
@Test
public void test2() throws Exception{
String str = "{\"name\":\"张三\",\"age\":23,\"addr\":\"北京\"}";
ObjectMapper mapper = new ObjectMapper();
Map readValue = mapper.readValue(str, Map.class);
System.out.println(readValue);
}
/**
* 将对象转换成json字符串
* {"name":"张三","age":23,"addr":"北京"}
* @throws Exception
*/
@Test
public void test3() throws Exception{
Person p = new Person("张三",23,"北京");
ObjectMapper mapper = new ObjectMapper();
String string = mapper.writeValueAsString(p);
System.out.println(string);
}
/**
* 将json字符串转换为对象
* Person [name=张三, age=23, addr=北京]
* @throws Exception
*/
@Test
public void test4() throws Exception{
String str = "{\"name\":\"张三\",\"age\":23,\"addr\":\"北京\"}";
ObjectMapper mapper = new ObjectMapper();
Person readValue = mapper.readValue(str, Person.class);
System.out.println(readValue);
}
/**
* 将数组转换成json字符串
* [{"name":"张三","age":23,"addr":"北京"},{"name":"李四","age":24,"addr":"北京"},{"name":"王五","age":25,"addr":"北京"}]
* @throws Exception
*/
@Test
public void test5() throws Exception{
Person p1 = new Person("张三",23,"北京");
Person p2 = new Person("李四",24,"北京");
Person p3 = new Person("王五",25,"北京");
Person[] persons = {p1,p2,p3};
ObjectMapper mapper = new ObjectMapper();
String string = mapper.writeValueAsString(persons);
System.out.println(string);
}
/**
* 将字符串转换成数组
* Person [name=张三, age=23, addr=北京] Person [name=李四, age=24, addr=北京] Person [name=王五, age=25, addr=北京]
* @throws Exception
*/
@Test
public void test6() throws Exception{
String str = "[{\"addr\":\"北京\",\"age\":23,\"name\":\"张三\"},{\"addr\":\"北京\",\"age\":24,\"name\":\"李四\"},{\"addr\":\"北京\",\"age\":25,\"name\":\"王五\"}]";
ObjectMapper mapper = new ObjectMapper();
Person[] persons = mapper.readValue(str,new TypeReference<Person[]>(){});
String content = "";
for (Person person : persons) {
content+=person.toString()+" ";
}
System.out.println(content);
}
/**
* 将集合转换成json字符串
* [{"name":"张三","age":23,"addr":"北京"},{"name":"李四","age":24,"addr":"北京"},{"name":"王五","age":25,"addr":"北京"}]
* @throws Exception
*/
@Test
public void test7() throws Exception{
List<Person> list = new ArrayList<Person>();
Person p1 = new Person("张三",23,"北京");
Person p2 = new Person("李四",24,"北京");
Person p3 = new Person("王五",25,"北京");
list.add(p1);
list.add(p2);
list.add(p3);
ObjectMapper mapper = new ObjectMapper();
String string = mapper.writeValueAsString(list);
System.out.println(string);
}
/**
* 将json字符串转换成集合
* [Person [name=张三, age=23, addr=北京], Person [name=李四, age=24, addr=北京], Person [name=王五, age=25, addr=北京]]
* @throws Exception
*/
@Test
public void test8() throws Exception{
String str = "[{\"addr\":\"北京\",\"age\":23,\"name\":\"张三\"},{\"addr\":\"北京\",\"age\":24,\"name\":\"李四\"},{\"addr\":\"北京\",\"age\":25,\"name\":\"王五\"}]";
ObjectMapper mapper = new ObjectMapper();
List<Person> persons = mapper.readValue(str,new TypeReference<List<Person>>(){});
System.out.println(persons);
}
/**
* 复杂数据的转换,map转json
* {"message":"传输数据","data":[{"name":"张三","age":23,"addr":"北京"},{"name":"李四","age":24,"addr":"北京"},{"name":"王五","age":25,"addr":"北京"}]}
* @throws Exception
*/
@Test
public void test9() throws Exception{
List<Person> list = new ArrayList<Person>();
Person p1 = new Person("张三",23,"北京");
Person p2 = new Person("李四",24,"北京");
Person p3 = new Person("王五",25,"北京");
list.add(p1);
list.add(p2);
list.add(p3);
Map<String,Object> map = new HashMap<String, Object>();
map.put("data", list);
map.put("message", "传输数据");
ObjectMapper mapper = new ObjectMapper();
String string = mapper.writeValueAsString(map);
System.out.println(string);
}
/**
* 复杂数据的转换,从json中得到传输数据
* [{name=张三, age=23, addr=北京}, {name=李四, age=24, addr=北京}, {name=王五, age=25, addr=北京}]
* @throws Exception
*/
@Test
public void test10() throws Exception{
String str = "{\"message\":\"传输数据\",\"data\":[{\"name\":\"张三\",\"age\":23,\"addr\":\"北京\"},{\"name\":\"李四\",\"age\":24,\"addr\":\"北京\"},{\"name\":\"王五\",\"age\":25,\"addr\":\"北京\"}]}";
ObjectMapper mapper = new ObjectMapper();
Map readValue = mapper.readValue(str,Map.class);
List list = (List) readValue.get("data");
System.out.println(list);
}
}
|