jackson和json-lib和Gson的比较
参考一下链接,jackson效率最高最快
jackson>Gson>json-lib
第一个实体类
package javawithjson;public class Company { String name; /*javabean规范指出,bean必须有一个空的构造方法,因为我们定义了构造方法Company(String string), * 默认空构造被覆盖了,所以必须手动制定空构造方法*/ public Company(){ } public Company(String string) { this.name=string; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Company [name=" + name + "]"; } }
第二个实体类,其中company引用了上边的Company类
package javawithjson;public class Person { String name; String sex; String address; Company c; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Company getC() { return c; } public void setC(Company c) { this.c = c; } @Override public String toString() { return "Person [name=" + name + ", sex=" + sex + ", address=" + address + ", c=" + c + "]"; } }
Junit中的测试代码
package javawithjson;import static org.junit.Assert.*;import java.io.IOException;import java.util.ArrayList;import java.util.Iterator;import org.codehaus.jackson.JsonGenerationException;import org.codehaus.jackson.map.JsonMappingException;import org.codehaus.jackson.map.ObjectMapper;import org.junit.Test;public class JsonTest { //注意,json转换对象是要求对象是Javabean的,entity要符合Javabean的规范 /* * JavaBean 类必须是一个公共类,并将其访问属性设置为 public ,如: public class user{......} * JavaBean 类必须有一个空的构造函数:类中必须有一个不带参数的公用构造器 * 一个javaBean类不应有公共实例变量,类变量都为private ,如: private int id; * 属性应该通过一组存取方法(getXxx 和 setXxx)来访问,一般是IDE(Eclipse、JBuilder) 为属性生成getter/setter 方法 */ //单个简单对象转换为json字符串 @Test public void test1() throws JsonGenerationException, JsonMappingException, IOException { Company c=new Company("香港大赌城"); Person p=new Person(); p.setName("黑帮老大"); p.setSex("man"); p.setAddress("监狱"); p.setC(c); ObjectMapper om=new ObjectMapper();//得到转换器 String json=om.writeValueAsString(p);//单个简单对象转换json字符串、 System.out.println(json); //{"address":"监狱","name":"黑帮老大","sex":"man","c":{"name":"香港大赌城"}} } @Test public void test2() throws JsonGenerationException, JsonMappingException, IOException{// Company c=new Company("香港大赌城"); Person p=new Person(); p.setName("黑帮老大"); p.setSex("man"); p.setAddress("监狱"); p.setC(c); ObjectMapper om=new ObjectMapper();//得到转换器 String json=om.writeValueAsString(p);//单个简单对象转换json字符串 System.out.println(json); Person p2=om.readValue(json, Person.class);//json字符串转对象 System.out.println(p2); } @Test public void test3() throws JsonGenerationException, JsonMappingException, IOException{ //arraylist转json字符串 Company c1=new Company("香港大赌城"); Company c2=new Company("什么鬼公司"); Company c3=new Company("腾讯科技"); ArrayListalc=new ArrayList (); alc.add(c1); alc.add(c2); alc.add(c3); ObjectMapper om=new ObjectMapper();//得到转换器对象 String json=om.writeValueAsString(alc);//arraylist转json(生成了数组) System.out.println(json); /* * [{"name":"香港大赌城"},{"name":"香港大赌城"},{"name":"香港大赌城"}] */ //json字符串转arraylist JavaType javaType = om.getTypeFactory().constructParametricType(List.class, Company.class); ArrayList alc2=om.readValue(json, javaType); Iterator it = alc2.iterator(); while(it.hasNext()){ System.out.println(it.next()); } }}
html读取json,生成了对象
无标题文档