标签:jackson String age json import java public name
1.1. 下载jackson
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
1.2. Java对象转换为json
Person.java |
package jackson;
import java.io.Serializable;
public class Person implements Serializable{
/**
*
*/
private static final longserialVersionUID = 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
this.age
this.sex
}
public String getName() {
return name;
}
public void setName(String name) {
this.name
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.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。
1.3. Map集合转换为json
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>();
"name", "morris");
"age", "22");
"address", "hk");
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(map);
System.out.println(jsonString);
}
}
|
运行结果如下:
{"address":"hk","age":"22","name":"morris"}
1.4. List集合转换为json
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>();
"name");
"age");
"address");
"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"]
1.5. 其他对象转换为json
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);
"a", "b", "c"
"hello world jackson!";
// byte
jsonGenerator.writeBinary(str.getBytes());
// boolean
jsonGenerator.writeBoolean(true);
// null
jsonGenerator.writeNull();
// float
jsonGenerator.writeNumber(2.2f);
// char
"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
// {
"user");// user:{
"name", "jackson");// name:jackson
"sex", true);// sex:true
"age", 22);// age:22
// }
"infos");// infos:[
// 22
"this is array");// this is array
// ]
// }
Person person = new Person();
"22");
"morris");
"男");
// complex Object
// {
"user", person);// user:{bean}
"infos", arr);// infos:[array]
// }
}
}
|
运行结果如下:
"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"]
标签:jackson,
String,
age,
json,
import,
java,
public,
name
From: https://blog.51cto.com/morris131/6216895