fastJSON的简单使用(相对于jackSON更精简)

导入fastJson包

 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>

编写Conroller



package com.Google.controller;

import com.Google.pojo.User;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//@Controller
@RestController//这个注解不会走视图解析器,会直接返回一个字符串
public class jsonController {
    @RequestMapping("/j1")
    //@ResponseBody//这个注解配合@Controller一起使用达到RestController的效果
    public String json1() throws JsonProcessingException {
        //
        ObjectMapper mapper = new ObjectMapper();
        User user = new User("Spring", 2, "女");
        String str = mapper.writeValueAsString(user);
        return str;
    }
    //传递一个数组
    @RequestMapping("/j2")
    public String json2() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        List<User> users = new ArrayList<>();
        User user = new User("Spring", 2, "女");
        User user1 = new User("Spring", 2, "女");
        User user2 = new User("Spring", 2, "女");
        User user3 = new User("Spring", 2, "女");
        users.add(user);
        users.add(user1);
        users.add(user2);
        users.add(user3);
        return mapper.writeValueAsString(users);
    }

    //传递一个时间对象
    /*@RequestMapping("/j3")
    public String json3() throws JsonProcessingException {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
        String format = dateFormat.format(date);
        return new ObjectMapper().writeValueAsString(format);
    }*/
    @RequestMapping("/j3")
    public String json3() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        //将时间戳的方式关闭
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        Date date = new Date();
        //自定义时间格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
        mapper.setDateFormat(dateFormat);
        return mapper.writeValueAsString(date);
    }
    @RequestMapping("/j4")
    public String json4() throws JsonProcessingException {
        List<User> users = new ArrayList<>();
        User user = new User("Spring", 2, "女");
        User user1 = new User("Spring1", 21, "女");
        User user2 = new User("Spring2", 22, "女");
        User user3 = new User("Spring3", 23, "女");
        users.add(user);
        users.add(user1);
        users.add(user2);
        users.add(user3);

        //两类型之间互相转换
        String jsonString = JSON.toJSONString(users);
        System.out.println("java对象转换成JSON字符串:"+jsonString);
        ArrayList jsonObject = JSON.parseObject(jsonString, ArrayList.class);
        System.out.println("JSON字符串转换成java对象:"+jsonObject);
        //将java对象转JSON对象
        JSONObject json = (JSONObject) JSON.toJSON(user);
        System.out.println("JSON对象name属性的值:"+json.getString("name"));
        //将JSON对象转java对象
        User user4 = JSON.toJavaObject(json, User.class);
        System.out.println("java对象的name属性值:"+user4.getSex());


        return "ha";
    }
}

java对象转换成JSON字符串

String jsonString = JSON.toJSONString(users);

JSON字符串转换成java对象

ArrayList jsonObject = JSON.parseObject(jsonString, ArrayList.class);

将java对象转JSON对象

JSONObject json = (JSONObject) JSON.toJSON(user);

将JSON对象转java对象

User user4 = JSON.toJavaObject(json, User.class);
posted @ 2022-02-12 14:24  小罗要有出息  阅读(116)  评论(0编辑  收藏  举报