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

2 篇文章 0 订阅

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;
    }

}

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值