Jackson是java中用来处理json的类库
主页为
推荐网页,里面有jackson的大量材料
http://www.cowtowncoder.com/blog/archives/2011/02/entry_443.html
Json常用的类库如下
Jackson:http://jackson.codehaus.org/
JSON-lib:http://json-lib.sourceforge.net/
Gson:http://code.google.com/p/google-gson/
性能最好的是Jackson,本人没有测试,看的是人的测试数据。
参考网址:
http://wangym.iteye.com/blog/738933
http://hjg1988.iteye.com/blog/561368
jackson中最重要的类为ObjectMapper,文档中的注释为可重用,可共享的。应该是线程安全的类。功能为实体类变为json和把json变为实体类,实体类变为json,ObjectMapper对象只把public的类成员变量或者有get方法的 变量写为json串(set方法可以没有).
ObjectMapper
Maven工程依赖如下
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.4</version>
</dependency>
Example1
import java.io.IOException;
import org.codehaus.jackson.map.ObjectMapper;
/**
*
* @version 1.0
* @author rjgcx
* @time 2012-3-24 21:38
*/
public class User {
int age;
int name;
public int getName() {
return name;
}
public void setName(int name) {
this.name = name;
}
public static void main(String[] args) throws IOException {
String s = "{\"name\":123}";
ObjectMapper mapper = new ObjectMapper();
//unmarshalling (reading JSON)
User a = mapper.readValue(s, User.class);
//marshalling (reading JSON)
//{"name":123}
System.out.println(mapper.writeValueAsString(a));
}
}