Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json、xml转换成Java对象。相比json-lib框架,Jackson所依赖的jar包较少,简单易用并且性能也要相对高些。而且Jackson社区相对比较活跃,更新速度也比较快。
下载地址: http://jackson.codehaus.org/1.7.6/jackson-all-1.7.6.jar
Person.java |
package jackson;
import java.io.Serializable;
public class Person implements Serializable{
/** * */ private static final long serialVersionUID = 1L;
private String name;
private String age;
@Override public String toString() { return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]"; }
public Person() { super(); // TODO Auto-generated constructor stub }
private String sex;
public Person(String name, String age, String sex) { super(); this.name = name; this.age = age; this.sex = sex; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getAge() { return age; }
public void setAge(String age) { this.age = age; }
public String getSex() { return sex; }
public void setSex(String sex) { this.sex = sex; }
}
|
Java2json.java |
package jackson;
import java.io.IOException;
import org.codehaus.jackson.JsonEncoding; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.map.ObjectMapper;
public class Java2Json {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
JsonGenerator generator = mapper.getJsonFactory().createJsonGenerator(System.out,JsonEncoding.UTF8);
Person person = new Person(); person.setName("morris"); person.setAge("22"); person.setSex("男");
generator.writeObject(person);
System.out.println("----------------");
mapper.writeValue(System.out, person);
}
} |
运行结果如下:
{"sex":"男","age":"22","name":"morris"}----------------
{"sex":"男","age":"22","name":"morris"}
上面分别利用JsonGenerator的writeObject方法和ObjectMapper的writeValue方法完成对Java对象的转换,二者传递的参数及构造的方式不同;JsonGenerator的创建依赖于ObjectMapper对象。也就是说如果你要使用JsonGenerator来转换JSON,那么你必须创建一个ObjectMapper。但是你用ObjectMapper来转换JSON,则不需要JSONGenerator。
Map2Json.java |
package jackson;
import java.io.IOException; import java.util.HashMap; import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper;
public class Map2Json {
public static void main(String[] args) throws IOException { Map<String, String> map = new HashMap<String, String>();
map.put("name", "morris"); map.put("age", "22"); map.put("address", "hk");
ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.writeValueAsString(map);
System.out.println(jsonString); }
}
|
运行结果如下:
{"address":"hk","age":"22","name":"morris"}
List2json.java |
package jackson;
import java.io.IOException; import java.util.ArrayList; import java.util.List;
import org.codehaus.jackson.JsonEncoding; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.map.ObjectMapper;
public class List2Json {
public static void main(String[] args) throws IOException {
List<String> list = new ArrayList<String>();
list.add("name"); list.add("age"); list.add("address"); list.add("sex");
ObjectMapper mapper = new ObjectMapper();
JsonGenerator generator = mapper.getJsonFactory().createJsonGenerator(System.out,JsonEncoding.UTF8);
generator.writeObject(list); System.out.println(); System.out.println("----------------"); //直接将集合转换为json字符串 System.out.println(mapper.writeValueAsString(list));
mapper.writeValue(System.out, list); }
}
|
运行结果如下:
["name","age","address","sex"]
----------------
["name","age","address","sex"]
["name","age","address","sex"]
Other2Json.java |
package jackson;
import java.io.IOException;
import org.codehaus.jackson.JsonEncoding; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.node.JsonNodeFactory;
public class Other2Json {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
JsonGenerator jsonGenerator = mapper.getJsonFactory().createJsonGenerator(System.out,JsonEncoding.UTF8);
String[] arr = { "a", "b", "c" }; String str = "hello world jackson!"; // byte jsonGenerator.writeBinary(str.getBytes()); // boolean jsonGenerator.writeBoolean(true); // null jsonGenerator.writeNull(); // float jsonGenerator.writeNumber(2.2f); // char jsonGenerator.writeRaw("c"); // String jsonGenerator.writeRaw(str, 5, 10); // String jsonGenerator.writeRawValue(str, 5, 5); // String jsonGenerator.writeString(str); jsonGenerator.writeTree(JsonNodeFactory.instance.POJONode(str)); System.out.println();
// Object jsonGenerator.writeStartObject();// { jsonGenerator.writeObjectFieldStart("user");// user:{ jsonGenerator.writeStringField("name", "jackson");// name:jackson jsonGenerator.writeBooleanField("sex", true);// sex:true jsonGenerator.writeNumberField("age", 22);// age:22 jsonGenerator.writeEndObject();// }
jsonGenerator.writeArrayFieldStart("infos");// infos:[ jsonGenerator.writeNumber(22);// 22 jsonGenerator.writeString("this is array");// this is array jsonGenerator.writeEndArray();// ]
jsonGenerator.writeEndObject();// }
Person person = new Person(); person.setAge("22"); person.setName("morris"); person.setSex("男"); // complex Object jsonGenerator.writeStartObject();// { jsonGenerator.writeObjectField("user", person);// user:{bean} jsonGenerator.writeObjectField("infos", arr);// infos:[array] jsonGenerator.writeEndObject();// } }
}
|
运行结果如下:
"aGVsbG8gd29ybGQgamFja3NvbiE="true null 2.2c world jac worl"hello world jackson!" "hello world jackson!"
{"user":{"name":"jackson","sex":true,"age":22},"infos":[22,"thisis array"]}{"user":{"age":"22","sex":"男","name":"morris"},"infos":["a","b","c"]
原文地址:http://blog.csdn.net/u022812849/article/details/43371459