使用json-org包實現POJO和json的轉換


使用json-org包實現POJO和json的轉換

這個jar包把對象轉換成json超級舒服,所以順便記錄一下吧

把單個pojo對象轉換成json對象

        Student student = new Student("2015551404","熊大");
        JSONObject object = new JSONObject(student);
        System.out.println(object.toString(2));

 

把多個pojo對象轉成json數組

        Student student = new Student("2015551404","熊大");
        Student student1 = new Student("2015551405", "孫嘉寧");
        ArrayList<Student> list = new ArrayList<>();
        list.add(student);
        list.add(student1);
        JSONArray array = new JSONArray(list);
        System.out.println(array.toString(2));

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM