jackson 用法

package com.test;


import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.JavaType;

public class Test {
	 private static JsonGenerator jsonGenerator = null;
	 static ObjectMapper mapper = new ObjectMapper();
	public static void main(String[] args) {
		User user  = new User();
		user.setUserId("1");
		user.setUserName("张三");
		user.setDob(new Date());
		
		
		User user2  = new User();
		user2.setUserId("2");
		user2.setUserName("李四");
		user2.setDob(new Date());
		
		
		Writer strWriter =  new StringWriter();
		try {
			mapper.writeValue(strWriter, user);
			String userDataJson = strWriter.toString();
			System.out.println("userDataJson======="+userDataJson);
		} 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();
		}
		
		String userJson =	"{\"userId\":\"1\",\"userName\":\"张三\",\"dob\":1408603469143}"; 
		try {
			User newUser = mapper.readValue(userJson, User.class);
			
			System.out.println("userId===="+newUser.getUserId()+" username = "+ newUser.getUserName()+" date=="+ newUser.getDob());
		} 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();
		}
		
		
		List<User> userList =new ArrayList<User>();
		userList.add(user);
		userList.add(user2);
		try {
			System.out.println("1###" + mapper.writeValueAsString(userList));
		} catch (JsonGenerationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (JsonMappingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		 try {
	            jsonGenerator = mapper.getJsonFactory().createJsonGenerator(System.out, JsonEncoding.UTF8);
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
		try {
			jsonGenerator.writeObject(userList);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String userJsonList = "[{\"userId\":\"1\",\"userName\":\"张三\",\"dob\":1408621742842},{\"userId\":\"2\",\"userName\":\"李四\",\"dob\":1408621742842}]";
		
		try {
			JavaType javaType = getCollectionType(ArrayList.class, User.class); 
			List<User> listUser = mapper.readValue(userJsonList, javaType);
			for (int i = 0; i < listUser.size(); i++) {
				User u = listUser.get(i);
				System.out.println("id==="+u.getUserId()+" uname ==="+ u.getUserName()+" date=="+u.getDob());
			}
			System.out.println(listUser.size());
		} 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 JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {   
		          return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);   
		  }   
}

你可能感兴趣的:(Jackson)