Java——》List、JSONObejct、JSONArray、Map

版权声明:本文为博主原创文章,无需授权即可转载,甚至无需保留以上版权声明,转载时请务必注明作者。
https://blog.youkuaiyun.com/weixin_43453386/article/details/83626103

以下JSONObject 和 JSONArray都是用的com.alibaba.fastjson

一、JSONObejct

1、JSONObejct转String:value == null的不输出

jsonObject.toJSONString()

import com.alibaba.fastjson.JSONObject;
import static com.alibaba.fastjson.serializer.SerializerFeature.WriteMapNullValue;

public class Test {
    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("aaa","aaa");
        jsonObject.put("bbb",111);
        jsonObject.put("ccc",null);
        jsonObject.put("ddd","");
        //{"aaa":"aaa","bbb":111,"ddd":""}
        System.out.println(jsonObject.toJSONString());
    }
}

2、JSONObejct转String:value == null的正常输出

jsonObject.toJSONString(jsonObject, WriteMapNullValue)

import com.alibaba.fastjson.JSONObject;
import static com.alibaba.fastjson.serializer.SerializerFeature.WriteMapNullValue;

public class Test {
    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("aaa","aaa");
        jsonObject.put("bbb",111);
        jsonObject.put("ccc",null);
        jsonObject.put("ddd","");
        //{"aaa":"aaa","ccc":null,"bbb":111,"ddd":""}
        System.out.println(jsonObject.toJSONString(jsonObject, WriteMapNullValue));
    }
}

3、JSONObejct转Map:value == null的不输出

JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference<Map<String, String>>(){})

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("aaa","aaa");
        jsonObject.put("bbb",111);
        jsonObject.put("ccc",null);
        jsonObject.put("ddd","");
        Map<String, String> params = JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference<Map<String, String>>(){});
        //{aaa=aaa, bbb=111, ddd=}
        System.out.println(params);
    }
}

4、JSONObejct转Map:value == null的正常输出

JSONObject.parseObject(JSONObject.toJSONString(jsonObject, WriteMapNullValue), new TypeReference<Map<String, String>>(){})

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import java.util.Map;
import static com.alibaba.fastjson.serializer.SerializerFeature.WriteMapNullValue;

public class Test {
    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("aaa","aaa");
        jsonObject.put("bbb",111);
        jsonObject.put("ccc",null);
        jsonObject.put("ddd","");
        Map<String, String> params = JSONObject.parseObject(JSONObject.toJSONString(jsonObject, WriteMapNullValue), new TypeReference<Map<String, String>>(){});
        //{aaa=aaa, bbb=111, ddd=}
        System.out.println(params);
    }
}

5、JSONObejct转Object对象

JSONObject.parseObject(a.toString(), Test.class)

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;

public class Main {
    public static void main(String[] args) {
        JSONObject a = new JSONObject();
        a.put("a",1);
        a.put("b",2);
        Test test = (Test) JSON.toJavaObject(a,Test.class);
        System.out.println(test.toString());
    }
}
@Data
class Test{
    public int a;
    public int b;
}


6、JSONObejct字符串转Object对象

JSONObject.parseObject(a.toString(), Test.class)

import com.alibaba.fastjson.JSONObject;
import lombok.Data;

public class Main {
    public static void main(String[] args) {
        JSONObject a = new JSONObject();
        a.put("a",1);
        a.put("b",2);
        Test test = JSONObject.parseObject(a.toString(), Test.class);
        System.out.println(test.toString());
    }
}
@Data
class Test{
    public int a;
    public int b;
}

7、String转JSONObejct

JSONObject.parseObject(str)

import com.alibaba.fastjson.JSONObject;

public class Test {
    public static void main(String[] args) {
        String str = "{\"result\":\"success\",\"message\":\"成功!\"}";
        JSONObject json = JSONObject.parseObject(str);
        //{"result":"success","message":"成功!"}
        System.out.println(json);
    }
}

8、Map转JSONObejct

JSONObject.parseObject(JSON.toJSONString(map))

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("aaa","aaa");
        map.put("bbb",111);
        map.put("ccc",null);
        map.put("ddd","");
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(map));
        //{"aaa":"aaa","bbb":111,"ddd":""}
        System.out.println(jsonObject);
    }
}

9、Object对象转JSONObject

JSONObject jsonObject = (JSONObject)JSON.toJSON(javaObject);

二、JSONArray

1、JSONArray转String

jsonArray.toString()

import com.alibaba.fastjson.JSONArray;

public class Test {
    public static void main(String[] args) {
        JSONArray jsonArray = new JSONArray();
        System.out.println(jsonArray.toString());
    }
}

2、JSONArray转List

JSONArray.parseArray(jsonArray.toString(), AAA.class)

import com.alibaba.fastjson.JSONArray;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        String str = "[{\"name\":\"Tom\",\"age\":\"20\"},{\"name\":\"Bob\",\"age\":\"11\"}]";
        JSONArray jsonArray = JSONArray.parseArray(str);
        List<AAA> aaaList = JSONArray.parseArray(jsonArray.toString(), AAA.class);
        aaaList.stream().forEach(e-> System.out.println(e.getName()+","+e.getAge()));
    }
}

class AAA {
    private String name;
    private int age;
    public AAA(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

3、String转JSONArray

JSONArray.parseArray(str)

import com.alibaba.fastjson.JSONArray;

public class Test {
    public static void main(String[] args) {
        String str = "[{\"name\":\"Tom\",\"age\":\"20\"},{\"name\":\"Bob\",\"age\":\"11\"}]";
        JSONArray jsonArray = JSONArray.parseArray(str);
        //[{"name":"Tom","age":"20"},{"name":"Bob","age":"11"}]
        System.out.println(jsonArray.toString());
    }
}

4、List转JSONArray

JSONArray.parseArray(JSON.toJSONString(aaaList))

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<AAA> aaaList = new ArrayList<>();
        aaaList.add(new AAA("Tom",20));
        aaaList.add(new AAA("Bob",11));
        JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(aaaList));
        //[{"name":"Tom","age":20},{"name":"Bob","age":11}]
        System.out.println(jsonArray);
    }
}

class AAA {
    private String name;
    private int age;
    public AAA(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值