java json解析jar包基本使用記錄

LoveHaloK發表於2018-07-26

一、使用json-lib.jar包處理

/*--------------------------------------------json字串--->json物件----------------------------------------------------*/
/**
 *  簡單的json解析
 */
public static void test1() {
    String jString = "{'name':'lisi','age':18}";
    try {
        JSONObject jsonObject = new JSONObject(jString);
        String name = jsonObject.getString("name");
        int age = jsonObject.getInt("age");
        Student student = new Student(name, age);
        System.out.println(student);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

/**
 *   包含物件的
 */
public static void test2() {

    String jString = "{'name':'lisi','age':18,'score':{'math':100,'chinese':90}}";

    try {
        JSONObject jsonObject = new JSONObject(jString);
        String name = jsonObject.getString("name");
        int age  = jsonObject.getInt("age");

        JSONObject scoreObj = jsonObject.getJSONObject("score");
        int math = scoreObj.getInt("math");
        int chinese = scoreObj.getInt("chinese");

        Student student = new Student(name, age,new Score(math, chinese));
        System.out.println(student);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
/**
 * 字串陣列轉轉換
 */
public static void  test3() {

    String jString = "{'studentList':[{'name':'張山','age':19,'score':{'math':'39','chinese':'50'}},{'name':'耿釗','age':17,'score':{'math':'68','chinese':'95'}}]}";
    try {
        JSONObject jsonObject = new JSONObject(jString);
        String arrStr = jsonObject.getString("studentList");
        JSONArray array = new JSONArray(arrStr);

        ArrayList<Student> list = new ArrayList<>();
        JSONObject object = null;
        Student student = null;
        Score score = null;

        for (int i = 0; i < array.length(); i++) {
            object = array.getJSONObject(i);

            String name = object.getString("name");
            int age = object.getInt("age");

            JSONObject scoreObj = object.getJSONObject("score");
            int math = scoreObj.getInt("math");
            int chinese = scoreObj.getInt("chinese");

            score = new Score(math, chinese);
            student = new Student(name, age, score);

            list.add(student);
        }
        //列印輸出
        for (Student student2 : list) {
            System.out.println(student2);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
/*--------------------------------------------物件--->json字串----------------------------------------------------*/

/**
 * 簡單物件轉json字串物件
 * @throws Exception 
 */
public static void test4() throws Exception {

    Student student = new Student("lisi", 23);

    JSONObject jsonObject = new JSONObject(student);

    System.out.println(jsonObject.getString("name"));
}

/**
 * 複雜物件轉json字串物件
 */
public static void test5() {

    Student student = new Student("耿釗", 18, new Score(67, 89));

    JSONObject jsonObject = new JSONObject(student);

    System.out.println(jsonObject);
}

/**
 * 陣列轉json
 */
public static void test6() {

    ArrayList< Student> list = new ArrayList<>();

    Student stu1 = new Student("lisi", 18, new Score(100, 100));
    Student stu2 = new Student("耿釗", 28, new Score(80, 90));

    list.add(stu1);
    list.add(stu2);

    Message message = new Message(list, 1000);

//JSONArray jsonArray = new JSONArray(list);
    JSONObject jsonObject  = new JSONObject(message);

    System.out.println(jsonObject);
    }
}

二、使用gson-2.2.1.jar

public class GsonTest {

    /**
     * 字串轉換成物件
     */
    public static void test1() {

        String jsString = "{'name':lisi','age':18}";

        Gson gson = new Gson();

        Student student = gson.fromJson(jsString, Student.class);

        System.out.println(student);
    }

    /**
     * 複雜字串轉換
     */
    public static void test2() {

        String jString = "{'name':'lisi','age':18,'score':{'math':100,'chinese':100}}";

        Gson gson = new Gson();

        Student student = gson.fromJson(jString, Student.class);

        System.out.println(student);
    }

    /**
     * 轉成集合
     */
    public static void test3() {

        String jString = "[{'name':'lisi','age':18,'score':{'math':100,'chinese':100}},"
                + "{'name':'lisi','age':18,'score':{'math':100,'chinese':100}},"
                + "{'name':'lisi','age':18,'score':{'math':100,'chinese':100}}]";

        Gson gson = new Gson();

        // 下面這種方式行不通
        /*
         * ArrayList<Student> list = gson.fromJson(jString, ArrayList.class);
         * for (Student student : list) { System.out.println(student); }
         */
        // TypeToken<ArrayList<Student>> token = new
        // TypeToken<ArrayList<Student>>(){};
        TypeToken<ArrayList<Student>> typeToken = new TypeToken<ArrayList<Student>>() {
        };
        ArrayList<Student> list = gson.fromJson(jString, typeToken.getType());

        for (Student student : list) {
            System.out.println(student);
        }
    }

    /**
     * 將java物件轉成json字串
     */
    public static void test4() {

        Student student = new Student("lisi", 18, new Score(100, 100));

        Gson gson = new Gson();

        System.out.println(gson.toJson(student));

    }

    /**
     * 將java集合物件轉成json
     */
    public static void test5() {

        Student student1 = new Student("lisi", 18, new Score(100, 100));
        Student student2 = new Student("耿釗", 10, new Score(100, 100));
        Student student3 = new Student("penghui", 19, new Score(100, 100));

        ArrayList<Student> list = new ArrayList<>();

        list.add(student1);
        list.add(student2);
        list.add(student3);
        Gson gson = new Gson();
        System.out.println(gson.toJson(list));
        ;
    }

三、使用fastjson-1.1.22.jar

public class FastJson {

/**
 * json字串轉換成物件
 * 
 *注意:物件定義有參建構函式後,要顯示宣告無參建構函式,否則會拋異常、
 */
public static void test1() {

    String jString = "{'name':'lisi','age':18,'score':{'math':100,'chinese':100}}";

    Student student = JSON.parseObject(jString,Student.class);

    System.out.println(student);
}
/**
 * json字串轉換成陣列集合
 */
public static void test2() {

    String jString = "[{'name':'lisi','age':18,'score':{'math':100,'chinese':100}},"
            + "{'name':'lisi','age':18,'score':{'math':100,'chinese':100}},"
            + "{'name':'lisi','age':18,'score':{'math':100,'chinese':100}}]";

    List<Student> list = JSON.parseArray(jString, Student.class);

    for (Student student : list) {
        System.out.println(student);
    }
}

/**
 * java物件轉換成json字串;
 */
public static void test3() {

    Student student = new Student("lisi", 18, new Score(100,199));

    String string = JSON.toJSONString(student);

    System.out.println(string);
}

public static void test4() {

    Student student1 = new Student("lisi", 18, new Score(100, 100));
    Student student2 = new Student("耿釗", 10, new Score(100, 100));
    Student student3 = new Student("penghui", 19, new Score(100, 100));

    ArrayList<Student> list = new ArrayList<>();

    list.add(student1);
    list.add(student2);
    list.add(student3);

    System.out.println(JSON.toJSONString(list));

}

總結

以上為個人學習筆記,僅供參考!

相關文章