Java与JSON之间的相互转换-4

添加 jar 包支持


jackson-annotations-2.0.5.jar
jackson-core-2.0.5.jar
jackson-databind-2.0.5.jar

编写工具类

import java.io.IOException;
import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

public final class JsonUtils {

    private static ObjectMapper mapper = new ObjectMapper();

    public static String toJson(Object data) throws JsonProcessingException {
        return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(data);
    }

    public static <T> T toObject(String data, Class<T> clazz) throws IOException {
        return mapper.readValue(data, clazz);
    }

    public static <T> List<T> toList(String data, Class<T> clazz) throws IOException {
        JavaType beanListType = mapper.getTypeFactory().constructParametricType(List.class, clazz);
        return mapper.readValue(data,  beanListType);
    }
}

测试代码

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import model.Person;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import util.JsonUtils;

public class JsonTest {

    /**
     * Object ---> JSON
     * @throws JsonProcessingException 
     */
    @Test
    public void testObjectToJson() throws JsonProcessingException {
        Person p = new Person();
        p.setName("张三");
        p.setAge("18");
        p.setAddress("郑州");

        String result = JsonUtils.toJson(p);
        System.out.println("=== Object ---> JSON ===");
        System.out.println(result);
    }

    /**
     * JSON ---> Object
     * @throws IOException 
     */
    @Test
    public void testJsonToObject() throws IOException {
        String data = "{\"address\":\"郑州\",\"age\":\"18\",\"name\":\"张三\"}";
        Person p = JsonUtils.toObject(data, Person.class);
        System.out.println("=== JSON ---> Object ===");
        System.out.println(p);
        System.out.println();
    }

    /**
     * List ---> JSON
     * @throws JsonProcessingException 
     */
    @Test
    public void testListToJson() throws JsonProcessingException {
        Person p1 = new Person();
        p1.setName("张三");
        p1.setAge("18");
        p1.setAddress("郑州");
        Person p2 = new Person();
        p2.setName("李四");
        p2.setAge("20");
        p2.setAddress("北京");

        List<Person> list = new ArrayList<Person>();
        list.add(p1);
        list.add(p2);

        String result = JsonUtils.toJson(list);
        System.out.println("=== List ---> JSON ===");
        System.out.println(result);
        System.out.println();
    }

    /**
     * JSON ---> List
     * @throws IOException 
     */
    @Test
    public void testJsonToList() throws IOException {
        String data = "[{\"address\":\"郑州\",\"age\":\"18\",\"name\":\"张三\"},{\"address\":\"北京\",\"age\":\"20\",\"name\":\"李四\"}]";

        List<Person> list = JsonUtils.toList(data, Person.class);
        System.out.println("=== JSON ---> List ===");
        for (Person person : list) {
            System.out.println(person);
        }
    }
}

运行结果

=== JSON ---> Object ===
Person [name=张三, age=18, address=郑州]

=== List ---> JSON ===
[ {
  "name" : "张三",
  "age" : "18",
  "address" : "郑州"
}, {
  "name" : "李四",
  "age" : "20",
  "address" : "北京"
} ]

=== JSON ---> List ===
Person [name=张三, age=18, address=郑州]
Person [name=李四, age=20, address=北京]
=== Object ---> JSON ===
{
  "name" : "张三",
  "age" : "18",
  "address" : "郑州"
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值