Ajax学习(3)Jackson使用

下载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());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值