jackson - 生成jason工具-简单示例

主页:

http://jackson.codehaus.org/

https://github.com/FasterXML/jackson

当前jackson分为三部分,需要分别下载;

jackson-annotations-2.5.0.jar,jackson-core-2.5.0.jar,jackson-databind-2.5.0.jar

java bean:

package com.srie.test.json;

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;

public class TestJSON {

    public static void main(String[] args) throws IOException {
        User user1 = new User(1, "a");
        User user2 = new User(2, "b");
        ObjectMapper mapper = new ObjectMapper();
        // mapper.writeValue(System.out, user1);
        System.out.println(mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(user1));
        String string = mapper.writeValueAsString(user2);
        System.out.println(string);
    }

}

class User {

    private int id;
    private String name;

    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + "]";
    }

    public User() {
    }

    public User(int id, String name) {
        super();
        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;
    }

}

 

你可能感兴趣的:(Jackson)