zoukankan      html  css  js  c++  java
  • FastJson学习:JSON格式字符串、JSON对象及JavaBean之间的相互转换

    当前台需要传送一系列相似数据到后端时,可以考虑将其组装成json数组对象,然后转化为json形式的字符串传输到后台

    例如:

                nodes = $('#PmPbsSelect_tree').tree('getChecked');
                var data=[];
                for(var i=0;i<nodes.length;i++){
                    if(!isParentCheck(nodes[i],nodes)){
                        data.push({"id":nodes[i].id,
                            "pid":node.id});
                    }else{
                        data.push({"id":nodes[i].id,
                            "pid":null});
                    }
                }    
                dataStr=JSON.stringify(data);    
                $.ajax({
                     url:ctx+"/PmWbs/savePmWbsByModel",
                     type:"POST",
                     data:{"dataStr":dataStr,
                         "projectId":pmProjectSelect.combobox('getValue')},
                     success:function(data){
                         basetree.tree('reload');
                     },
                     error:function(){
                         alert("请求失败");
                     },
                     dataType:"json"
                 });      

    接下来阐述正文:(其实是copy其他人的博客,主要放在自己博客下面好找...........................)

    ==================华丽的分割线=======================================

    fastJson对于json格式字符串的解析主要用到了一下三个类:

    JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换。

    JSONObject:fastJson提供的json对象。

    JSONArray:fastJson提供json数组对象。

    首先定义了三个json格式的字符串作为我们的数据源

    //json字符串-简单对象型
    private static final String  JSON_OBJ_STR = "{"studentName":"lily","studentAge":12}";
    //json字符串-数组类型
    private static final String  JSON_ARRAY_STR = "[{"studentName":"lily","studentAge":12},{"studentName":"lucy","studentAge":15}]";
    //复杂格式json字符串
    private static final String  COMPLEX_JSON_STR = "{"teacherName":"crystall",
    "teacherAge":27,"course":{"courseName":"english","code":1270},
    "students":[{"studentName":"lily","studentAge":12},{"studentName":"lucy","studentAge":15}]}";

    一、JSON格式字符串与JSON对象之间的转换。

    1.json字符串-简单对象型 与 JSONObject之间的转换

    JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR);
    //因为JSONObject继承了JSON,所以这样也是可以的
    //JSONObject jsonObject1 = JSONObject.parseObject(JSON_OBJ_STR); 

    2.json字符串-数组类型与JSONArray之间的转换

    JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR);
    //因为JSONArray继承了JSON,所以这样也是可以的
    //JSONArray jsonArray1 = JSONArray.parseArray(JSON_ARRAY_STR);
    
            //遍历方式1
            int size = jsonArray.size();
            for (int i = 0; i < size; i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
            }
    
            //遍历方式2
            for (Object obj : jsonArray) {
                JSONObject jsonObject = (JSONObject) obj;
            }
        

    3.复杂json格式字符串与JSONObject之间的转换

    JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
    //因为JSONObject继承了JSON,所以这样也是可以的
    //JSONObject jsonObject1 = JSONObject.parseObject(COMPLEX_JSON_STR);
            
            String teacherName = jsonObject.getString("teacherName");
            Integer teacherAge = jsonObject.getInteger("teacherAge");
            JSONObject course = jsonObject.getJSONObject("course");
            JSONArray students = jsonObject.getJSONArray("students");

     此时获取到了json字符串的数组,再对各个字符串进行解析

    二、JSON格式字符串与javaBean之间的转换

    首先,我们针对数据源所示的字符串,提供三个javaBean。

    public class Student {
    
        private String studentName;
        private Integer studentAge;
    
        public String getStudentName() {
            return studentName;
        }
    
        public void setStudentName(String studentName) {
            this.studentName = studentName;
        }
    
        public Integer getStudentAge() {
            return studentAge;
        }
    
        public void setStudentAge(Integer studentAge) {
            this.studentAge = studentAge;
        }
    }
    
    
    
    
    public class Course {
    
        private String courseName;
        private Integer code;
    
        public String getCourseName() {
            return courseName;
        }
    
        public void setCourseName(String courseName) {
            this.courseName = courseName;
        }
    
        public Integer getCode() {
            return code;
        }
    
        public void setCode(Integer code) {
            this.code = code;
        }
    }
    
    
    
    public class Teacher {
    
        private String teacherName;
        private Integer teacherAge;
        private Course course;
        private List<Student> students;
    
        public String getTeacherName() {
            return teacherName;
        }
    
        public void setTeacherName(String teacherName) {
            this.teacherName = teacherName;
        }
    
        public Integer getTeacherAge() {
            return teacherAge;
        }
    
        public void setTeacherAge(Integer teacherAge) {
            this.teacherAge = teacherAge;
        }
    
        public Course getCourse() {
            return course;
        }
    
        public void setCourse(Course course) {
            this.course = course;
        }
    
        public List<Student> getStudents() {
            return students;
        }
    
        public void setStudents(List<Student> students) {
            this.students = students;
        }
    }

    json字符串与javaBean之间的转换推荐使用 TypeReference<T> 这个类,使用泛型可以更加清晰,当然也有其它的转换方式,这里就不做探讨了。

    1.json字符串-简单对象型与javaBean之间的转换

    Student student = JSON.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {}); 
    //因为JSONObject继承了JSON,所以这样也是可以的
     //Student student1 = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});

    2.json字符串-数组类型与javaBean之间的转换

    ArrayList<Student> students = JSON.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});
    //因为JSONArray继承了JSON,所以这样也是可以的
    //ArrayList<Student> students1 = JSONArray.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});
  • 相关阅读:
    LeetCode 485. Max Consecutive Ones
    LeetCode 367. Valid Perfect Square
    LeetCode 375. Guess Number Higher or Lower II
    LeetCode 374. Guess Number Higher or Lower
    LeetCode Word Pattern II
    LeetCode Arranging Coins
    LeetCode 422. Valid Word Square
    Session 共享
    java NIO
    非阻塞IO
  • 原文地址:https://www.cnblogs.com/houchen/p/11203888.html
Copyright © 2011-2022 走看看