fastjson

GitHub

https://github.com/alibaba/fastjsonjava

fastjson是目前java語言中最快的json庫,比 jacksongson 都要快。git

Maven

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.49</version>
</dependency>

Methods

Object → String

將 VO 轉爲 Stringgithub

將 Map 轉爲 Stringjson

將 Collection 轉爲 String數組

將 Array 轉爲 Stringmaven

String jsonString = JSON.toJSONString(object)

String → T 或 Map<String, Object>

將 String 轉爲 Tgoogle

VO vo = JSON.parseObject(text, VO.class)

將 String 轉爲 JSONObject(即 Map<String, Object>)code

JSONObject object = JSON.parseObject(text)

String → List< T > 或 List< Object >

將 String 轉爲 List 對象

List<VO> list= JSONObject.parseArray(text, VO.class);

將 String 轉爲 JSONArray(即 List< Object >)get

JSONArray array = JSON.parseArray(text)

Samples

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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import hust.zeng.utils.pojo.Group;
import hust.zeng.utils.pojo.User;

public class JsonUtil {

    public static void main(String[] args) {

        System.out.println("1.Object >> String");

        // 1.1.將普通對象轉爲JsonString
        Group group = new Group();
        group.setId(1L);
        group.setName("group1");

        User guestUser = new User();
        guestUser.setId(10L);
        guestUser.setName("admin");

        User rootUser = new User();
        rootUser.setId(11L);
        rootUser.setName("guest");

        group.addUser(guestUser);
        group.addUser(rootUser);

        String voString = JSON.toJSONString(group);
        System.out.println(voString);

        // 1.2.將Map對象轉爲JsonString
        HashMap<String, Object> map = new HashMap<>(); //JSONObject map = new JSONObject();
        map.put("id", 7);
        HashMap<String, Object> info = new HashMap<>();
        info.put("name", "HUST");
        info.put("adreess", "wuhan");
        map.put("info", info);
        String mapStr = JSON.toJSONString(map);
        System.out.println(mapStr);

        // 1.3.將List對象轉爲JsonString
        ArrayList<String> list = new ArrayList<>(); //JSONArray list = new JSONArray();
        list.add("aa");
        list.add("bb");
        String listStr = JSON.toJSONString(list);
        System.out.println(listStr);

        // 1.4.將數組對象轉爲JsonString
        Object[] array = new Object[3];
        array[0] = 1;
        array[1] = 2;
        array[2] = 3;
        String arrayStr = JSON.toJSONString(array);
        System.out.println(arrayStr);

        System.out.println("2.String → T 或 Map<String, Object>");

        // 2.1.將String轉爲T
        Group group2 = JSON.parseObject(voString, Group.class);
        System.out.println(group2);

        // 2.2.將String轉爲JSONObject
        Map<String, Object> map2 = JSON.parseObject(mapStr);
        System.out.println(map2);

        System.out.println("3.String → List<T> 或 List<Object>");
        
        // 3.1.將String轉爲List<T>
        List<String> list2 = JSON.parseArray(listStr, String.class);
        System.out.println(list2);

        // 3.2.將String轉爲JSONArray
        List<Object> array2 = JSON.parseArray(arrayStr);
        System.out.println(array2);

    }
}
相關文章
相關標籤/搜索