Java 进阶 & JackJson 的使用

需要包:
jackson-core-2.2.3.jar(核心jar包)
jackson-annotations-2.2.3.jar(该包提供Json注解支持)
jackson-databind-2.2.3.jar


    com.fasterxml.jackson.core
    jackson-core
    2.9.1


    com.fasterxml.jackson.core
    jackson-annotations
    2.9.1


    com.fasterxml.jackson.core
    jackson-databind
    2.9.1

1、指定对象Class转成 json字符串
User user = new User();
user.setName("小民");
user.setEmail("[email protected]");
user.setAge(20);

SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
user.setBirthday(dateformat.parse("1996-10-01"));

/**

  • ObjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectMapper中实现。
  • ObjectMapper有多个JSON序列化的方法,可以把JSON字符串保存File、OutputStream等不同的介质中。
  • writeValue(File arg0, Object arg1)把arg1转成json序列,并保存到arg0文件中。
  • writeValue(OutputStream arg0, Object arg1)把arg1转成json序列,并保存到arg0输出流中。
  • writeValueAsBytes(Object arg0)把arg0转成json序列,并把结果输出成字节数组。
  • writeValueAsString(Object arg0)把arg0转成json序列,并把结果输出成字符串。
    */
    ObjectMapper mapper = new ObjectMapper();

//User类转JSON

{"name":"小民","age":20,"birthday":844099200000,"email":"[email protected]"}
String json = mapper.writeValueAsString(user);
System.out.println(json);

输出:
{"name":"小民","age":20,"birthday":844099200000,"email":"[email protected]"}

2、List集合转化成json字符串
List users = new ArrayList();
users.add(user);
String jsonlist = mapper.writeValueAsString(users);
System.out.println(jsonlist);

输出:

[{"name":"小民","age":20,"birthday":844099200000,"email":"[email protected]"}]
3、Json字符串转化成指定Class类
String json = "{\"name\":\"小民\",\"age\":20,\"birthday\":844099200000,\"email\":\"[email protected]\"}";
/**
 * ObjectMapper支持从byte[]、File、InputStream、字符串等数据的JSON反序列化。
 */
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(json, User.class);
System.out.println(user);

输出:

User{name='小民aa', age=25, birthday=Tue Oct 01 00:00:00 CST 1996, email='[email protected]'}
4、Json字符串转化成集合List

方法一:

String jsonString="[{'id':'1'},{'id':'2'}]";
ObjectMapper mapper = new ObjectMapper();
JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, Bean.class);
//如果是Map类型  mapper.getTypeFactory().constructParametricType(HashMap.class,String.class, Bean.class);  
List lst =  (List)mapper.readValue(jsonString, javaType);

输出:

Student{name='s1', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s2', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s3', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s4', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s5', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s6', age=12, date=Thu Jun 09 20:38:37 CST 2016}

方法二:

String jsonString="[{'id':'1'},{'id':'2'}]";
ObjectMapper mapper = new ObjectMapper();
List beanList = mapper.readValue(jsonString, new TypeReference>() {});

输出:

Student{name='s1', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s2', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s3', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s4', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s5', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s6', age=12, date=Thu Jun 09 20:38:37 CST 2016}

你可能感兴趣的:(Java 进阶 & JackJson 的使用)