使用.alibaba.fastjson包进行json数据的转换!

参考:https://www.cnblogs.com/cdf-opensource-007/p/7106018.html

以下是对于json简单的格式和数据格式,还有就是包含前2种格式的第三格式

package Fastjson;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FastjsonDemo{

        //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}]}";


    public static void main(String[] args) {
        //简单的json格式转换
        testJSONorObject();
        //json转换成Map
        testJSONonMap();
        //json数组格式转换成
        testJSONStrToJSONArray();
        //复杂的json格式转换
        testComplexJSONStrToJSONObject();

    }

    //json字符串转换成json对象
    public static void testJSONorObject(){
        JSONObject jsonObject =JSON.parseObject(JSON_OBJ_STR

                        );
        //JSONObject jsonObject1 = JSONObject.parseObject(JSON_OBJ_STR); //因为JSONObject继承了JSON,所以这样也是可以的
        System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
    }
    //json字符串转换成Map集合
    public static  void testJSONonMap(){
        Map<String,String> map=JSON.parseObject(JSON_OBJ_STR,Map.class);
        for (String  str:map.keySet()) {
            System.out.println(str);
        }
    }

    //json数组格式转换成
    public static  void  testJSONStrToJSONArray(){
        JSONArray jsonArray =JSON.parseArray(JSON_ARRAY_STR);
        //JSONArray jsonArray1 = JSONArray.parseArray(JSON_ARRAY_STR);//因为JSONArray继承了JSON,所以这样也是可以的

        //遍历方式1
        int size=jsonArray.size();
        //遍历方式1
        for (int i = 0; i < size; i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
        }

        //遍历方式2
        for (Object obj : jsonArray) {
            JSONObject jsonObject = (JSONObject) obj;
            System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
        }
    }


    /**
     * 复杂json格式字符串与JSONObject之间的转换
     */
    public static void testComplexJSONStrToJSONObject(){

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

        String teacherName = jsonObject.getString("teacherName");
        Integer teacherAge = jsonObject.getInteger("teacherAge");
        JSONObject course = jsonObject.getJSONObject("course");
        JSONArray students = jsonObject.getJSONArray("students");
    }




}


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.List;


public class JSONorBeanTest {

    //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}]}";



    public static void main(String[] args) {
        testJSONtoJAVABEAN();
        testJSONStrToJavaBeanList();
        testComplexJSONStrToJavaBean();
    }

    /**
     * json字符串-简单对象与JavaBean_obj之间的转换
     */
    public static  void testJSONtoJAVABEAN(){
        Student student = JSON.parseObject(JSON_OBJ_STR,Student.class);
        //Student student1 = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});//因为JSONObject继承了JSON,所以这样也是可以的
        System.out.println(student.getStudentName()+":"+student.getStudentAge());
       /* Student student=new Student();
        student.setStudentAge(23);
        student.setStudentName("hah");

        System.out.println( JSON.toJSONString(student));*/
    }

    /**
     * json字符串-数组类型与JavaBean_List之间的转换
     */
    public static void testJSONStrToJavaBeanList(){
        ArrayList<Student> students = JSON.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});
        //ArrayList<Student> students1 = JSONArray.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});//因为JSONArray继承了JSON,所以这样也是可以的
        for (Student student : students) {
            System.out.println(student.getStudentName()+":"+student.getStudentAge());
        }
    }
    /**
     * 复杂json格式字符串与JavaBean_obj之间的转换
     */
    public static void testComplexJSONStrToJavaBean(){
        Teacher teacher = JSON.parseObject(COMPLEX_JSON_STR, new TypeReference<Teacher>() {});
        //Teacher teacher1 = JSON.parseObject(COMPLEX_JSON_STR, new TypeReference<Teacher>() {});//因为JSONObject继承了JSON,所以这样也是可以的
        String teacherName = teacher.getTeacherName();
        Integer teacherAge = teacher.getTeacherAge();
        Course course = teacher.getCourse();
        List<Student> students = teacher.getStudents();
    }
}

以下是关于json于bean的操作

猜你喜欢

转载自blog.csdn.net/weixin_41244495/article/details/85566089