AI 摘要

文章标题:JSON常用转换 摘要:本文介绍了常见的JSON转换操作,包括将Java对象转换为JSON对象和JSON字符串,将JSON对象转换为JSON字符串和Java对象,以及将JSON字符串转换为JSON对象和Java对象的方法。代码示例详细演示了每种转换操作的实现步骤,并附有相应的输出结果。读者可以根据需求选择适合的方法进行JSON转换操作。

Json转换:

1、Java对象-->Json对象:

public class JSON2JavaTest{
    public static void main(String[] args) {
        Student stu = new Student("公众号BiggerBoy", "m", 2);
        //Java对象转化为JSON对象
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(stu);
        System.out.println("Java对象转化为JSON对象\n" + jsonObject);//{"name":"公众号编程大道","age":2,"sex":"m"}
    }
}

2、Java对象-->Json字符串:

public class JSON2JavaTest{
    public static void main(String[] args) {
        Student stu = new Student("公众号BiggerBoy", "m", 2);
        //先转成JSON对象
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(stu);
        //JSON对象转换为JSON字符串
        String jsonString = jsonObject.toJSONString();
        System.out.println("JSON对象转换为JSON字符串\n" + jsonString);//{"name":"公众号编程大道","age":2,"sex":"m"}
    }
}

3、Json对象-->Json字符串:

public class JSON2JavaTest{
    public static void main(String[] args) {
        Student stu = new Student("公众号BiggerBoy", "m", 2);
        //先转成JSON对象
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(stu);
        //JSON对象转换为JSON字符串
        String jsonString = jsonObject.toJSONString();
        System.out.println("JSON对象转换为JSON字符串\n" + jsonString);//{"name":"公众号编程大道","age":2,"sex":"m"}

    }
}

4、Json对象-->Java对象:

public class JSON2JavaTest{
    public static void main(String[] args) {
        Student stu = new Student("公众号BiggerBoy", "m", 2);
        //先转成JSON对象
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(stu);
        //JSON对象转换成Java对象
        Student student = JSONObject.toJavaObject(jsonObject, Student.class);
        System.out.println("JSON对象转换成Java对象\n" + student);//Student{name='公众号编程大道', sex='m', age=2}
    }
}

5、Json字符串-->Json对象:

public class JSON2JavaTest{
    public static void main(String[] args) {
        String stuString = "{\"age\":2,\"name\":\"公众号 BiggerBoy\",\"sex\":\"m\"}";
        //JSON字符串转换成JSON对象
        JSONObject jsonObject1 = JSONObject.parseObject(stuString);
        System.out.println("JSON字符串转换成JSON对象\n" + jsonObject1);//{"sex":"m","name":"公众号编程大道","age":2}
    }
}

6、Json字符串-->Java对象:

public class JSON2JavaTest{
    public static void main(String[] args) {
        String stuString = "{\"age\":2,\"name\":\"公众号 BiggerBoy\",\"sex\":\"m\"}";
        //JSON字符串转换成Java对象
        Student student1 = JSONObject.parseObject(stuString, Student.class);
        System.out.println("JSON字符串转换成Java对象\n" + student1);//Student{name='公众号编程大道', sex='m', age=2}
    }
}

7、Json字符串-->List:

public class JSON2JavaTest{
    public static void main(String[] args) {
        String stuString = "[{\"age\":2,\"name\":\"公众号\",\"sex\":\"m\"},{\"age\":18,\"name\":\"BiggerBoy\",\"sex\":\"m\"}]";
        //JSON字符串转换成Java对象
        List<Student> studentList = JSONObject.parseArray(stuString, Student.class);
        System.out.println("JSON字符串转换成Java对象list\n" +studentList );   } }
最后更新于 2023-09-26