1. 程式人生 > 其它 >fastJSON的簡單使用(相對於jackSON更精簡)

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