参考资料
1 java中几个json工具分析测试【原】
http://blog.csdn.net/weinianjie1/article/details/6561788
2 两款JSON类库Jackson与JSON-lib的性能对比(新增第三款测试)
http://wangym.iteye.com/blog/738933
基于上面文章所说,jackson的性能高于其它二个json库,但gson的使用方式对我们来说是最好理解的,仿照gson提供的二个方法,以下对jackson作了一个包装,另外有个org.json.jar包就没有用了,是对源代码进行打包的,也上传上来吧,以后可能用得上的
环境:XP+JDK1.6
版本:jackson-all-1.8.1.jar
工程图片如下:
JacksonFactory.java
package net.liuzd.json;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class JacksonFactory {
private static final ObjectMapper mapper = new ObjectMapper();
private JacksonFactory(){
}
/**
* @param <T> 泛型声明
* @param bean 类的实例
* @return JSON字符串
*/
public static <T> String toJson(T bean){
StringWriter sw = new StringWriter();
try {
JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
mapper.writeValue(gen, bean);
gen.close();
return sw.toString();
} catch (JsonGenerationException e) {
throw new RuntimeException(e.getMessage(),e);
} catch (JsonMappingException e) {
throw new RuntimeException(e.getMessage(),e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(),e);
}
}
/**
* @param <T> 泛型声明
* @param json JSON字符串
* @param clzz 要转换对象的类型
* @return
*/
public static <T> T fromJson(String json,Class<T> clzz){
T t = null;
try {
t = mapper.readValue(json, clzz);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return t;
}
/**
* @param json JSON字符串,请保持key是加了双引号的
* @return Map对象,默认为HashMap
*/
public static Map<?,?> fromJson(String json){
try {
return mapper.readValue(json, HashMap.class);
} catch (JsonParseException e) {
throw new RuntimeException(e.getMessage(),e);
} catch (JsonMappingException e) {
throw new RuntimeException(e.getMessage(),e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(),e);
}
}
}
TestJson.java(测试类)
package net.liuzd.json;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class TestJson {
public static void main(String[] args) {
String json;
List<Book> books = new ArrayList<Book>();
Book b1 = new Book();
b1.setId(1);
b1.setName("book1");
Book b2 = new Book();
b2.setId(2);
b2.setName("book2");
books.add(b1);
books.add(b2);
Order order = new Order();
order.setBooks(books);
order.setCost(100.5f);
order.setName("orderName"); order.setDate(new Date(System.currentTimeMillis()));
json = JacksonFactory.toJson(order);
System.out.println("order: " + json);
order = JacksonFactory.fromJson(json, Order.class);
System.out.println(order.getName()+","+order.getCost() + "," + order.getDate());
for(Book b : order.getBooks()){
System.out.println(b.getId() + "," + b.getName());
}
Users bean = new Users("1","张三",23);
json = JacksonFactory.toJson(bean);
System.out.println(json);
Users bean2 = JacksonFactory.fromJson(json, Users.class);
System.out.println(bean2.getId() + "," + bean2.getUsername() + "," + bean2.getAge());
Map<?,?> map = JacksonFactory.fromJson("{\"a\":1, \"b\":2}");
System.out.println(map.get("a"));
System.out.println(map.get("b"));
json = JacksonFactory.toJson(map);
System.out.println(json);
}
}
输出结果如下:
order: {"name":"orderName","date":1318986605676,"cost":100.5,"books":[{"name":"book1","id":1},{"name":"book2","id":2}]}
orderName,100.5,Wed Oct 19 09:10:05 CST 2011
1,book1
2,book2
{"id":"1","username":"张三","age":23}
1,张三,23
1
2
{"b":2,"a":1}
其它的一些类就不写了吧!
工程中没有包含这二个jar,如下