Bootstrap

开发中json的各种转换

项目中可能会经常涉及到json的各种转换,下面是常用方法。

我们要使用的是 com.alibaba.fastjson.JSONObject
引入依赖

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

创建一个Users类

public class Users {
    public Users(){

    }
public Users(int uid, String uname, String upassword, String uemail, String usex, int ustatus, String ucode, int urole) {
        this.uid = uid;
        this.uname = uname;
        this.upassword = upassword;
        this.uemail = uemail;
        this.usex = usex;
        this.ustatus = ustatus;
        this.ucode = ucode;
        this.urole = urole;
    }

    private int uid; // 用户实体的主键属性
    private String uname; // 用户账号
    private String upassword; // 用户密码
    private String uemail; // 用户邮箱 用于激活使用
    private String usex; // 用户性别
    private int ustatus; // 用户激活状态 0 未激活 1 激活
    private String ucode; // 邮件激活码
    private int urole; // 用户 0 管理员 1

public getXXX()....
public setXXX()....

    @Override
    public String toString() {
        return "Users{" +
                "uid=" + uid +
                ", uname='" + uname + '\'' +
                ", upassword='" + upassword + '\'' +
                ", uemail='" + uemail + '\'' +
                ", usex='" + usex + '\'' +
                ", ustatus=" + ustatus +
                ", ucode='" + ucode + '\'' +
                ", urole=" + urole +
                '}';
    }
}

注意:如果要使用Users类转为JSONObject对象或者JSONObject对象转为Users对象,都会涉及到Users类的get和set方法。所以,如果Users类没有get或set方法,则转换不会报错,但是无效。

测试类

public class TestUsers {
    public static void main(String[] args) {
        Users users = new Users(2022, "coderitl", "coderitl", "[email protected]", "男", 1, "2022", 2);
       ........
    }

一、将实体类转为json字符串

String usersJsonStr = JSONObject.toJSONString(users);
System.out.println(usersJsonStr);

输出

{"ucode":"2022","uemail":"[email protected]","uid":2022,"uname":"coderitl","upassword":"coderitl","urole":2,"usex":"男","ustatus":1}

二、将实体类转为json对象

JSONObject usersJsonObject =(JSONObject) JSONObject.toJSON(users);
System.out.println(usersJsonObject);

输出

{"upassword":"coderitl","uid":2022,"ustatus":1,"uname":"coderitl","usex":"男","ucode":"2022","urole":2,"uemail":"[email protected]"}

三、json对象转某个对象

Users users1 = JSONObject.parseObject(usersJsonObject.toJSONString(), Users.class);
System.out.println(users1);

输出

Users{uid=2022, uname='coderitl', upassword='coderitl', uemail='123xxx@qq.com', usex='男', ustatus=1, ucode='2022', urole=2}

注意:这个方法会出现一个问题,某些值会丢失。替代方法,先转为Map,然后直接通过get取值

四、json对象转某个对象升级版

Map<String,Users> map = JSONObject.parseObject(usersJsonObject.toJSONString(),Map.class);
Set<String> strings = map.keySet();
Iterator<String> iterator = strings.iterator();
        while(iterator.hasNext()){
            System.out.println(map.get(iterator.next()));
        }

输出

coderitl
2022
1
coderitl
男
2022
2
123xxx@qq.com

五、获取json对象的属性值

String unameStr = (String)usersJsonObject.get("uname");
System.out.println(unameStr);

输出

coderitl

六、list转JSAONArray

List<Users> users2= new ArrayList<>();
        for (int i=1;i<=100;i++){
            Users u = new Users(2022, "coderitl", "coderitl", "[email protected]", "男", 1, "2022", 2);
            users2.add(u);
        }
        JSONArray userJsonArray = JSONObject.parseArray(JSONObject.toJSONString(users2));
        System.out.println(userJsonArray);

输出

[{"upassword":"coderitl","uid":2022,"ustatus":1,"uname":"coderitl","usex":"男","ucode":"2022","urole":2,"uemail":"[email protected]"},{"upassword":"coderitl","uid":2022,"ustatus":1,"uname":"coderitl","usex":"男","ucode":"2022","urole":2,"uemail":"[email protected]"}]

七、map转Json

Map map1 = new HashMap();
        map1.put("one",users1);
        map1.put("two",users1);
        JSONObject mapJsonObject =(JSONObject) JSONObject.toJSON(map1);
        System.out.println(mapJsonObject);

输出

{"two":{"upassword":"coderitl","uid":2022,"ustatus":1,"uname":"coderitl","usex":"男","ucode":"2022","urole":2,"uemail":"[email protected]"},"one":{"upassword":"coderitl","uid":2022,"ustatus":1,"uname":"coderitl","usex":"男","ucode":"2022","urole":2,"uemail":"[email protected]"}}

八、将json字符串转为JSONObject对象

String jsonStr1 = "{\"upassword\":\"coderitl\",\"uid\":2022,\"ustatus\":1,\"uname\":\"coderitl\",\"usex\":\"男\",\"ucode\":\"2022\",\"urole\":2,\"uemail\":\"[email protected]\"}";
        JSONObject strJsonObject = JSONObject.parseObject(jsonStr1);
        System.out.println(strJsonObject);

输出

{"upassword":"coderitl","uid":2022,"ustatus":1,"uname":"coderitl","usex":"男","ucode":"2022","urole":2,"uemail":"[email protected]"}

九、将JSONObject对象转为json字符串

String strJson = strJsonObject.toJSONString();
        System.out.println(strJson);

输出

{"upassword":"coderitl","uid":2022,"ustatus":1,"uname":"coderitl","usex":"男","ucode":"2022","urole":2,"uemail":"[email protected]"}

十、总结

1、普通对象转为JSONObject对象,使用 toJSON 方法
2、JSONObject对象转为普通对象,使用 parseObject(String text, Class clazz)
3、json字符串转JSONObject对象,使用 parseOjbect(String text)
4、JSONOject对象转json字符串,使用 toJSONString 方法

;