下载Jackson:点击下载Jackson库
package com.atguigu.ajax.app.test;
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class Customer {
private int id;
private String name;
public Customer(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity(){
return "fuzhou";
}
public String getBirth(){
return "1991-12-29";
}
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
/**
* 1.导入jar包
* 2.创建ObjectMappter对象
* 3.调用mapper的writeValueAsString
* 4.注意:jackson使用getter方法来定位JSON对象属性
* 可以通过添加注解org.codehaus.jackson.annotate.JsonIgnore
* 来忽略某一个getter定义的属性
*/
ObjectMapper mapper = new ObjectMapper();
Customer customer = new Customer(1001, "AtGuigu");
String jsonStr = mapper.writeValueAsString(customer);
System.out.println(jsonStr);
}
}
输出:
{"id":1001,"name":"AtGuigu","city":"fuzhou","birth":"1991-12-29"}
package com.atguigu.ajax.app.test;
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class Customer {
private int id;
private String name;
public Customer(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
//修改getName-->getCustomerName
public String getCustomerName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity(){
return "fuzhou";
}
public String getBirth(){
return "1991-12-29";
}
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
/**
* 1.导入jar包
* 2.创建ObjectMappter对象
* 3.调用mapper的writeValueAsString
* 4.注意:jackson使用getter方法来定位JSON对象属性
* 可以通过添加注解org.codehaus.jackson.annotate.JsonIgnore
* 来忽略某一个getter定义的属性
*/
ObjectMapper mapper = new ObjectMapper();
Customer customer = new Customer(1001, "AtGuigu");
String jsonStr = mapper.writeValueAsString(customer);
System.out.println(jsonStr);
}
}
输出:
{"id":1001,"customerName":"AtGuigu","city":"fuzhou","birth":"1991-12-29"}
package com.atguigu.ajax.app.test;
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class Customer {
private int id;
private String name;
public Customer(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCustomerName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity(){
return "fuzhou";
}
//添加@JsonIgnore
@JsonIgnore
public String getBirth(){
return "1991-12-29";
}
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
/**
* 1.导入jar包
* 2.创建ObjectMappter对象
* 3.调用mapper的writeValueAsString
* 4.注意:jackson使用getter方法来定位JSON对象属性
* 可以通过添加注解org.codehaus.jackson.annotate.JsonIgnore
* 来忽略某一个getter定义的属性
*/
ObjectMapper mapper = new ObjectMapper();
Customer customer = new Customer(1001, "AtGuigu");
String jsonStr = mapper.writeValueAsString(customer);
System.out.println(jsonStr);
}
}
输出:
{"id":1001,"city":"fuzhou","customerName":"AtGuigu"}
Json工具类
package com.zzy.json;
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import com.zzy.model.Person;
import com.zzy.model.Phone;
/**
* JsonUtil.java 说明:json工具类
*
* @author zhengzy
* @date 2015年9月25日
*/
public class JsonUtils {
public static <T> T fromJson(String strJson, Class<T> className) {
try {
return new ObjectMapper().readValue(strJson, className);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String toJson(Object obj) {
try {
return new ObjectMapper().writeValueAsString(obj);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
/**
* model需要空的构造函数
*/
Person person = new Person(1, "xiaoming", new Phone("0594", "1234567890"));
System.out.println(JsonUtils.toJson(person));
Person person2 = JsonUtils.fromJson(JsonUtils.toJson(person), Person.class);
System.out.println(person2.toString());
}
}