`

jackson_

 
阅读更多
首先这个用起来是不错,貌似是说比json-lib还要快

java端User对象
package com.liao;

public class User <T extends Object>{
	public String name="wjg";
	public Object[] adress={"pudong","jiangsu"};
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Object[] getAdress() {
		return adress;
	}
	public void setAdress(Object[] adress) {
		this.adress = adress;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public int age=20;
	public String job="develop";

}

===============================================================

json2bean:
package com.liao;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class JacksonExample {

	public static void main(String[] args) {

		User user = new User();
		shanghai sh=new shanghai();
	//	user.adress[0]=sh.didian;
		ObjectMapper mapper = new ObjectMapper();
		StringWriter out=new StringWriter();
		JsonGenerator jg=null;
		/*
		 * 这是bean2json
		 * 当用JsonGenerator的时候注意要关掉流
		 */
		try {
			 jg=new JsonFactory().createJsonGenerator(out);
			mapper.writeValue(jg, user);
			System.out.println(out.toString());
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	
		try {
			jg.close();
			out.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		/*
		 * 这里只是bean2json
		 * 
		 */
//		try {
//
//			// convert user object to json string, and save to a file
//
//			mapper.writeValue(new File("H:\\json.txt"), user);
//
//			// display to console
//
//			System.out.println(mapper.defaultPrettyPrintingWriter().writeValueAsString(user));
//
//		} catch (JsonGenerationException e) {
//
//			e.printStackTrace();
//
//		} catch (JsonMappingException e) {
//
//			e.printStackTrace();
//
//		} catch (IOException e) {
//
//			e.printStackTrace();
//
//		}
//		
		
	 user=json2bean(mapper,User.class);
	 /*
	  * 这里是json2bean
	  * 上面调用的那个方法是对所有方法的封装,值得看,这个只是对User对象的转化
	  */
//	try {
//		user=	mapper.readValue(new File("H:\\json.txt"), User.class);
//		} 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();
//		}
		
		System.out.println(user.name+"  "+user.age+"   "+user.adress[1]+"   "+user.job);
	}
/*
 * 这里是json2bean
 * 该方法是封装了转化的所有对象
 * 
 */
	private static <T>T json2bean(ObjectMapper 
			 mapper,Class<T> class1) {
		// TODO Auto-generated method stub
		T ojb=null;
		try {
			ojb=	mapper.readValue(new File("H:\\json.txt"), class1);
			} 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();
			}
			
		return ojb;
	}
	public void ff(){
		
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics