Java中常用数据类型与JSON格式互转

Java中关于数据类型转JSON格式主要有两种常用的方法

1.通过Jackson


    com.fasterxml.jackson.core
    jackson-annotations
    2.9.2


    com.fasterxml.jackson.core
    jackson-core
    2.9.2


    com.fasterxml.jackson.core
    jackson-databind
    2.9.2
 

2.通过Json-lib


    net.sf.json-lib
    json-lib
    2.4
    jdk15

3.举例:

public class Student {
    private int id;
    private String name;
    private int age;

    private List interest;
    private List friends;
    private Map teachers;

}

public class Friend {
    private String name;
    private int age;
}
public class Teacher {
    private String name;
    private int age;
}
public class JsonDemo {
    public static void main(String[] args) throws IOException {
        // 定义一个集合用来存储兴趣信息
        List interest = new ArrayList();
        interest.add("读书");
        interest.add("跑步");
        interest.add("看电影");
        //定义一个集合用来存储朋友信息
        List friends = new ArrayList();
        friends.add(new Friend("小王",22));
        friends.add(new Friend("大王",22));
        friends.add(new Friend("小鬼",22));
        //定义一个map用来存储老师信息
        Map teachers = new HashMap();
        teachers.put("语文老师", new Teacher("嘻嘻",44));
        teachers.put("数学老师", new Teacher("哈哈",44));
        
        Student stu1 = new Student();
        stu1.setId(1);
        stu1.setName("张三");
        stu1.setAge(22);
        stu1.setInterest(interest);
        stu1.setFriends(friends);
        stu1.setTeachers(teachers);

        Student stu2 = new Student();
        stu2.setId(2);
        stu2.setName("李四");
        stu2.setAge(22);
        stu2.setInterest(interest);
        stu2.setFriends(friends);
        stu2.setTeachers(teachers);
        
        //利用ObjectMapper将javaBean转换成json字符串
        ObjectMapper om = new ObjectMapper();
        String stustr1 = om.writeValueAsString(stu1);
        String stustr2 = om.writeValueAsString(stu2);
        System.out.println(stustr1);
        System.out.println(stustr2);
        
        //利用JsonGenerator将javaBean转换成json字符串、
        JsonGenerator jg = null;
        jg = om.getJsonFactory().createJsonGenerator(System.out,JsonEncoding.UTF8);
        jg.writeObject(stu1);
        //测试的时候中文乱码,解决方式未知
        System.out.println();

        // 使用JSONArray将java对象转换为json
        JSONArray json1 = new JSONArray();
        // JSONArray是一个集合,通过这种方式添加的子对象都是JSONObject类型
        json1.add(stu1);
        json1.add(stu2);
        System.out.println("JSONArray:" + json1);

        // 使用JSONObject将java对象转换为json
        JSONObject obj = new JSONObject();
        obj = obj.fromObject(stu1);
        System.out.println("JSONObject:" + obj);
        // 调用JSONObject的get方法可以得到对应字段的值
        int id = obj.getInt("id");
        System.out.println("id:" + id);

        // 演示由JSONArray得到JSONObject对象
        obj = json1.getJSONObject(0);
        System.out.println("JSONObject:" + obj);

        // 演示如何将JSONArray对象转换成javaBean对象
        List students = new ArrayList();
        students.add(stu1);
        students.add(stu2);
        JSONArray json2 = new JSONArray();
        // 通过这种方式添加的每一个子对象都是JSONObject对象
        json2 = json2.fromObject(students);
        System.out.println("json2:" + json2);
        System.out.println("数据个数:" + json2.size());

        //在操作json的数据格式的时候,如果没有指明数据类型,那么只能是基本类型或者是String类型,不能出现复杂数据类型。
        for (int i = 0; i < json2.size(); i++) {
            JSONObject jobj = json2.getJSONObject(i);
            //当javaBean中含有比较复杂的数据结构时,如List,Map
            Map map = new HashMap();
            //此处的friends必须保证和javaBean中的属性名相同
            map.put("friends", Friend.class);
            Student student = (Student) JSONObject.toBean(jobj, Student.class,map);
            System.out.println("student" + i + ":" + student);
            Friend friend =(Friend) student.getFriends().get(0);
            Map teac =  student.getTeachers();
            //注意这样写会报类型转换错误ClassCastException
//            Teacher ttt = (Teacher)teac.get("语文老师");
            //应该这样写,再转一次
            Teacher teacher = (Teacher)JSONObject.toBean(JSONObject.fromObject(teac.get("语文老师")),Teacher.class);
            System.out.println("语文老师的名字:"+teacher.getName());
        }
     
        

        JSONArray json3 = new JSONArray();
        //字符串转jsonArray得满足JSONArray格式
        String str = "[{id:1,name:'张三'}]";
        json3 = json3.fromObject(str);
        System.out.println("str:"+json3);
        //字符串转JSONobject,得满足JSONObject格式
        JSONObject obj2 = new JSONObject();
        String str2 = "{id:1,name:'张三'}";
        obj2 = obj2.fromObject(str2);
        System.out.println("str2:"+obj2);
        
        //集合List,用上面定义的interest集合测试
        json3 = json3.fromObject(interest);
        System.out.println("interest:"+json3);
        String value = (String)json3.get(0);
        System.out.println("value:"+value);
        
        //map
        Map map1 = new HashMap();
        map1.put("id", "1");
        map1.put("name","王小坏");
        map1.put("age", "22");
        json3=json3.fromObject(map1);
        System.out.println("map:"+json3);

    }
    
}

 

你可能感兴趣的:(常用API用法总结)