欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java fastjson学习

程序员文章站 2022-06-17 09:06:31
...

在pom.xml导入

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

1.map转json

使用JSON.toJSON

   Map<String, String> map = new HashMap<>();
        map.put("one", "第一个");
        map.put("two", "第二个");
        System.out.println(JSON.toJSON(map));

输出

{"two":"第二个","one":"第一个"}

查看源码得知返回的类型是个JsonObject

java fastjson学习

2.List转json

使用JSON.toJSON

  List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("key1", "One");
        map1.put("key2", "Two");

        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("key1", "Three");
        map2.put("key2", "Four");

        list.add(map1);
        list.add(map2);
        System.out.println(JSON.toJSON(list));

输出

[{"key1":"One","key2":"Two"},{"key1":"Three","key2":"Four"}]

查看源码得知返回的类型是个JsonArray(先把List中的Map转成JsonObject,然后添加JsonObject到JsonArray中)

java fastjson学习

3.bean转Json

写个UserBean类

public class User {
    private String username;
    private int age;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

然后使用JSON.toJSON

   User user=new User();
        user.setUsername("userbean类");
        user.setAge(21);
        System.out.println(JSON.toJSON(user));

输出

{"age":21,"username":"userbean类"}

返回的是JsonObjectjava fastjson学习

4.JSON转bean

使用 JSON.parseObject(String text, Class<T> clazz)

 String jsonstr = "{\"age\":21,\"username\":\"userbean类\"}";
        User user=JSON.parseObject(jsonstr,User.class);
        System.out.println(user.getUsername()+"---->"+user.getAge());

输出

userbean类---->21

5.JSON转Map

使用  JSON.parseObject(String text, TypeReference<T> type)

 Map<String, Object> map = new HashMap<>();
        map.put("one", "第一个");
        map.put("two", "第二个");
        String jsonstr = JSON.toJSONString(map);
        System.out.println("JSON数据为" + jsonstr);

        Map<String,Object> parsemap = JSON.parseObject(jsonstr,new TypeReference<Map<String,Object>>(){});
        Iterator<Map.Entry<String,Object>> iterable =  parsemap.entrySet().iterator();
        while(iterable.hasNext()) {
            Map.Entry<String,Object> mapson = iterable.next();
            System.out.println(mapson.getKey()+"----"+mapson.getValue());
        }

输出

JSON数据为{"one":"第一个","two":"第二个"}
one----第一个
two----第二个

6.JSON转List

使用  JSON.parseObject(String text, TypeReference<T> type)

 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("key1", "One");
        map1.put("key3", "Two");
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("key1", "Three");
        map2.put("key2", "Four");

        list.add(map1);
        list.add(map2);
        String jsonstr =JSON.toJSONString(list);
        System.out.println("JSON数据为:"+jsonstr);

        List<Map<String,Object>> list1=JSON.parseObject(jsonstr, new TypeReference<List<Map<String,Object>>>(){});
        for (int i=0;i<list1.size();i++) {
            System.out.println(list1.get(i));
        }

输出:

JSON数据为:[{"key1":"One","key3":"Two"},{"key1":"Three","key2":"Four"}]
{key1=One, key3=Two}
{key1=Three, key2=Four}

7.map转jsonObject

使用 JSONObject.parseObject(String text)

  Map<String, Object> map = new HashMap<>();
        map.put("one", "第一个");
        map.put("two", "第二个");
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(map));
        jsonObject.put("three","第三个");
        System.out.println(jsonObject.toJSONString());

输出

{"one":"第一个","two":"第二个","three":"第三个"}

8.list转jsonarray

使用 JSONArray.parseArray(String text)

final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("key1", "One");
        map1.put("key3", "Two");
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("key1", "Three");
        map2.put("key2", "Four");

        list.add(map1);
        list.add(map2);
        JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(list));

        Map<String, Object> map = new HashMap<>();
        map.put("key1", "第一个");
        map.put("two", "第二个");
        JSONObject jsonObject = new JSONObject(map);
        jsonArray.add(jsonObject);

        for (int i = 0;i < jsonArray.size();i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            System.out.println(jsonObject1);
        }

输出

{"key1":"One","key3":"Two"}
{"key1":"Three","key2":"Four"}
{"key1":"第一个","two":"第二个"}

 

相关标签: fastjson