使用.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的操作

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章