使用Alibaba FastJson进行Object、List、Map与Json之间的相互转换

Talk is cheap, show you the code.
一般情况下,JSON中的转换方法与JSONObject中的转换方法没有区别。
查看源码,JSONObject继承了JSON类,但并没有重写toJsonString、parseObject等方法。

package com.jake.mallcrud;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jake.mallcrud.service.UserService;
import com.jake.mallmodel.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class JsonTests {

    @Autowired
    private UserService userService;

    /**
     * POJO对象转Json
     */
    @Test
    public void obj2Json() {
        User user = userService.findUserById(1);
        System.out.println("toString---" + user.toString());
        System.out.println("JSON.toJSONString---" + JSON.toJSONString(user));
        System.out.println("JSONObject.toJSONString---" + JSONObject.toJSONString(user));
    }

    /**
     * Json转POJO对象
     */
    @Test
    public void json2Obj() {
        User user = userService.findUserById(1);
        String userStr = JSON.toJSONString(user);
        System.out.println("JSON.parseObject---" + JSON.parseObject(userStr, User.class));
        System.out.println("JSONObject.parseObject---" + JSONObject.parseObject(userStr, User.class));
    }


    /**
     * POJO对象集合转Json
     */
    @Test
    public void list2Json() {
        List<User> users = userService.findAllUsers();
        System.out.println("toString---" + users.toString());
        System.out.println("JSON.toJSONString---" + JSON.toJSONString(users));
        System.out.println("JSONObject.toJSONString---" + JSONObject.toJSONString(users));
    }

    /**
     * Json转POJO对象集合
     */
    @Test
    public void json2List() {
        List<User> users = userService.findAllUsers();
        String usersStr = JSON.toJSONString(users);
        System.out.println("JSON.parseObject---" + JSON.parseObject(usersStr, List.class));
        System.out.println("JSONObject.parseObject---" + JSONObject.parseObject(usersStr, List.class));
    }

    /**
     * Map转Json
     */
    @Test
    public void map2Json() {
        Map<String, Object> map = getMap();
        System.out.println("JSON.toJSONString" + JSON.toJSONString(map));
        System.out.println("JSONObject.toJSONString" + JSONObject.toJSONString(map));
    }

    /**
     * Json转Map
     */
    @Test
    public void json2Map() {
        String mapStr = JSON.toJSONString(getMap());
        System.out.println("JSON.parseObject---" + JSON.parseObject(mapStr, Map.class));
        System.out.println("JSONObject.parseObject---" + JSONObject.parseObject(mapStr, Map.class));

    }

    private Map<String, Object> getMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("string", "Hello World!");
        map.put("int", 1);
        map.put("list", userService.findAllUsers());
        return map;
    }

}

猜你喜欢

转载自blog.csdn.net/qq_15329947/article/details/88076388