加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

【Android 应用开发】 FastJson 使用详解

发布时间:2020-12-16 18:58:54 所属栏目:百科 来源:网络整理
导读:博客地址 : http://blog.csdn.net/shulianghan/article/details/41011605 fastjson 源码地址 : -- GitHub : https://github.com/alibaba/fastjso n ; -- 示例源码 : https://github.com/alibaba/fastjson/wiki/Samples-DataBind ; -- Jar下载 :http://source

博客地址:http://blog.csdn.net/shulianghan/article/details/41011605


fastjson 源码地址:

--GitHub:https://github.com/alibaba/fastjson;

--示例源码:https://github.com/alibaba/fastjson/wiki/Samples-DataBind;

--Jar下载:http://sourceforge.net/projects/fastjson/;



总结 :


Java Bean <--> json 字符串 <--> JSONObject 互相转化


json 字符串 与 Java Bean 对象 互相转化 :

--json 字符串 -> Java Bean 对象:JSON.parSEObject(String text,Class<Student> clazz)方法,示例 -Student student = JSON.parSEObject(json_student,Student.class);

--Java Bean 对象 -> json 字符串:JSON.toJSONString(Object object,boolean prettyFormat)方法,示例 -String format_json = JSON.toJSONString(student,true);


Java Bean 对象 与 JSONObject 对象 互相转化:

--Java Bean 对象 -> JSONObject 对象:JSON.toJSON(Object javaObject)方法,0)">JSONObject object = (JSONObject) JSON.toJSON(student_s);

--JSONObject 对象 -> Java Bean 对象 (无直接方法实现):JSON.parSEObject(jsonObject.toString(),Class<T> clazz)方法,先转成字符串,再转成 Java Bean;


json 字符串 与 JSONObject 对象互相转化:

--json 字符串 -> JSONObject 对象:JSON.parSEObject(String text)方法,示例 -JSONObject object = JSON.parSEObject(json_student);

--JSONObject 对象 -> json 字符串:JSONObject.toString()方法,0)">object.toString();



Java Bean 对象 <--> json 字符串 <--> JSONArray 对象 互相转化


json 字符串 与 Java 集合 互相转化:

--json 字符串 -> Java 集合:JSON.parSEObject(String text,Class<Student> clazz)方法,0)">List<Student> students = JSON.parseArray(json_array,Student.class);

--Java 集合 -> json 字符串:toJSONString(Object object,204,0)">String json_array = JSON.toJSONString(students,0)">Java 集合 与 JSONArray 对象 互相转化:

--Java 集合 -> JSONArray 对象:JSON.toJSON(Object javaObject)方法,0)">JSONArray array = (JSONArray) JSON.toJSON(students);

--JSONArray 对象 -> Java 集合(没有直接转化方法) : 先转成 json 字符串,再转成 Java 集合;


json 字符串 与 JSONArray 对象 互相转化:

--json 字符串 -> JSONArray 对象:JSON.parseArray(String text)方法,0)">JSONArray array = JSON.parseArray(json_array);

--JSONArray 对象 -> json 字符串:jsonArray.toString() 方法;




一. JSON 简介



1. JSON 结构



(1) 对象


对象简介:

--格式: 对象定义在 大括号 {} 中,中间的项由 逗号 隔开,每个项目都是 key : value 进行的;

--示例:{"age":42,"id":1,"male":true,"name":"Sherlock Holmes","schoolId":1};

--格式化后的:

[plain] view plain copy
  1. {
  2. "age":42,
  3. "id":1,
  4. "male":true,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "name":"SherlockHolmes",248)"> "schoolId":1
  5. }



(2) 数组


数组简介:

--数组格式: 数组放在 中括号[] 中,在 [] 中有 n 个 {} 元素,每个元素用 逗号 隔开;

--数组示例:[{"age":42,"schoolId":1},{"age":42,"id":2,"male":false,"name":"John Watson","schoolId":1}] ;

--输出格式化后的数组:

copy
[
  • {
  • "age":42,248)"> "id":1,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "male":true,248)"> "name":"SherlockHolmes",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "schoolId":1
  • },108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> {
  • "id":2,248)"> "male":false,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "name":"JohnWatson",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> }
  • ]



  • 二. JSON 相关用法



    1.JSON.toJSONString 方法



    (1) JSON.toJSONString(Object object)方法简介


    JSON.toJSONString(Object object)方法简介:

    --方法定义:public static final String toJSONString(Object object);

    --方法作用: 将 java bean 转为 json 字符串;

    --方法位置:String com.alibaba.fastjson.JSON.toJSONString(Object object);

    --参数介绍: Object object -> 要转换的实体类对象;


    方法简介:

    --public static final String toJSONString(Object object,boolean prettyFormat);

    --方法作用:将 java bean 转为 json 字符串,并将 json 字符串格式化;

    --String com.alibaba.fastjson.JSON.toJSONString(Object object,255)">参数介绍:Object object-> 要转换的实体类对象,boolean prettyFormat-> 是否将 json 字符串格式化 如果 false 与 上面的方法作用相同;


    (2) JSON.toJSONString(Object object) 和 JSON.toJSONString(Object object,boolean prettyFormat)方法使用示例


    JavaBean 类定义:

    [java] copy
      packagecn.org.octopus.fastjson.beans;
    1. publicclassStudent{
    2. privateintid;
    3. privateStringname;
    4. intage;
    5. booleanisMale;
    6. intschoolId;
    7. publicStudent(){
    8. super();
    9. publicStudent(intid,Stringname,intage,153); font-weight:bold; background-color:inherit">booleanisMale,153); font-weight:bold; background-color:inherit">intschoolId){
    10. this.id=id;
    11. this.name=name;
    12. this.age=age;
    13. this.isMale=isMale;
    14. this.schoolId=schoolId;
    15. }
    16. intgetId(){
    17. returnid;
    18. voidsetId(intid){
    19. publicStringgetName(){
    20. returnname;
    21. voidsetName(Stringname){
    22. this.name=name;
    23. intgetAge(){
    24. returnage;
    25. voidsetAge(intage){
    26. booleanisMale(){
    27. returnisMale;
    28. voidsetMale(booleanisMale){
    29. this.isMale=isMale;
    30. intgetSchoolId(){
    31. returnschoolId;
    32. voidsetSchoolId(intschoolId){
    33. @Override
    34. publicStringtoString(){
    35. return"Student[id="+id+",name="+name+",age="+age
    36. +",isMale="+isMale+",schoolId="+schoolId+"]";
    37. }

    main 函数代码:

    copy
    staticvoidmain(String[]args){
  • //创建一个Student对象
  • Studentstudent=newStudent(1,"SherlockHolmes",42,153); font-weight:bold; background-color:inherit">true,0); background-color:inherit">1);
  • //将Student对象转为json字符串
  • Stringjson=JSON.toJSONString(student);
  • //打印json字符串
  • System.out.println(json);
  • //将Student对象转为json字符串,这个字符串是经过格式化的
  • Stringformat_json=JSON.toJSONString(student,true);
  • //打印格式化的json
  • System.out.println(format_json);
  • }

  • 执行结果 :

    --第一个结果: 该结果没有经过格式化,只是将 json 字符串输出;

    --第二个结果: 该结果按照 json 的格式进行格式化了;

    copy
    {"age":42,"name":"SherlockHolmes","schoolId":1}
  • }



  • 2.JSON.toJSON 方法



    (1)JSON.toJSON(Object javaObject) 方法简介


    方法简介:

    --public static final Object toJSON(Object javaObject);

    --方法作用: 将java bean 对象转为 JSONObject 对象,或者将集合 转为 JSONArray 对象;

    --Object com.alibaba.fastjson.JSON.toJSON(Object javaObject);

    --Object javaObject->java bean 对象或者集合;


    关键代码解析:

    --Java Bean 转 JSONObject:JSONObject object = (JSONObject) JSON.toJSON(student_s);

    --Java 集合 转 JSONArray:JSONArray array = (JSONArray) JSON.toJSON(students);


    JSONOject 和 JSONArray 打印: 这两种对象 打印本身,toString() 以及 toJSONString() 都是相同的结果,详见下面的示例;



    (2) 示例


    Student 实体类: 在 一. 1. (2) 中有定义,直接使用该实体类即可;


    Main() 示例:

    copy
    classMain{
  • //该字符串是{"age":42,"schoolId":1}注意将"转为"
  • finalStringjson_student="{"age":42,"id":1,"male":true,"name":"SherlockHolmes","schoolId":1}";
  • voidmain(String[]args){
  • //创建一个Student对象
  • Studentstudent_s=1);
  • //将javabean对象转为JSONObject对象
  • JSONObjectobject=(JSONObject)JSON.toJSON(student_s);
  • //JSONObject直接打印,打印toString()或者toJSONString()输出结果是一样的
  • System.out.println(object+"--打印JSONOBject本身");
  • System.out.println(object.toString()+"--打印JSONOBject.toString()");
  • System.out.println(object.toJSONString()+"--打印JSONOBject.toJSONString()");
  • //再创建一个Student对象
  • Studentstudent_j=2,"JohnWatson",153); font-weight:bold; background-color:inherit">false,0); background-color:inherit">//将两个Student对象放到List集合中
  • List<Student>students=newArrayList<Student>();
  • students.add(student_s);
  • students.add(student_j);
  • //将List集合对象转为JSONArray对象
  • JSONArrayarray=(JSONArray)JSON.toJSON(students);
  • //JSONArray直接打印,248)"> System.out.println(array+"--打印JSONArray本身");
  • System.out.println(array.toString()+"--打印JSONArray.toString()");
  • System.out.println(array.toJSONString()+"--打印JSONArray.toJSONString()");
  • }

  • 执行结果 :

    copy
  • {"age":42,"schoolId":1}--打印JSONOBject.toString()
  • {"age":42,"schoolId":1}--打印JSONOBject.toJSONString()
  • [{"age":42,"name":"JohnWatson","schoolId":1}]--打印JSONArray本身
  • [{"age":42,"schoolId":1}]--打印JSONArray.toString()



  • 3.JSON.parseArray 方法



    (1)JSON.parseArray 方法简介


    JSONArray parseArray(String text)方法简介:

    --public static final JSONArray parseArray(String text);

    --方法作用: 将 json 字符串转为 JSONArray 对象;

    --JSONArray com.alibaba.fastjson.JSON.parseArray(String text);

    --String text->json 字符串;


    <T> List<T> parseArray(String text,Class<T> clazz) 方法简介:

    --方法定义:public static final <T> List<T> parseArray(String text,Class<T> clazz);

    --方法作用: 将 json 字符串转为 List 集合;

    --<T> List<T> com.alibaba.fastjson.JSON.parseArray(String text,255)">参数介绍:String text->json 字符串,204)">Class<T> clazz->集合元素类型;



    (2) 代码示例


    Main() 代码:

    copy
    //字符串内容[{"age":42,"schoolId":1}]
  • finalStringjson_array="[{"age":42,"schoolId":1},{"age":42,"id":2,"male":false,"name":"JohnWatson","schoolId":1}]";
  • //将json字符串转为JSONArray对象
  • JSONArrayarray=JSON.parseArray(json_array);
  • //打印JSONArray对象
  • System.out.println(array);
  • //将json字符串转为List集合
  • List<Student>students=JSON.parseArray(json_array,Student.class);
  • //打印List集合大小
  • System.out.println("students.size():"+students.size());
  • //遍历List集合中的元素
  • for(Studentstudent:students)
  • System.out.println(student.toString());
  • copy
    [{"age":42,"schoolId":1}]
  • students.size():2
  • Student[id=1,name=SherlockHolmes,age=42,isMale=true,schoolId=1]
  • Student[id=2,name=JohnWatson,isMale=false,schoolId=1]



  • 4. JSON.parSEObject 方法



    (1)JSON.parSEObject 方法简介


    JSONObject parSEObject(String text) 方法简介:

    --public static final JSONObject parSEObject(String text);

    --方法作用: 将 json 字符串类型转化为 JSONObject 对象;

    --JSONObject com.alibaba.fastjson.JSON.parSEObject(String text);

    --String text->json 字符串;


    <T> T parSEObject(String text,Class<T> clazz) 方法简介:

    --public static final <T> T parSEObject(String text,255)">方法作用: 将 json 字符串转为指定类型的 java bean 对象;

    --<T> T com.alibaba.fastjson.JSON.parSEObject(String text,153)">json 字符串,204)">Class<T> clazz->要转化的 java bean 对象类型;



    (2) 方法示例


    Main() 方法:

    copy
    //将json字符串转为JSONObject对象
  • JSONObjectobject=JSON.parseObject(json_student);
  • //打印JSONObject对象
  • System.out.println(object);
  • //将json字符串转为Student对象
  • Studentstudent=JSON.parseObject(json_student,0); background-color:inherit">//打印Student对象
  • System.out.println(student.toString());
  • copy
    Student[id=1,schoolId=1]



  • 5.JSON.parse 方法



    (1)Object parse(String text) 方法简介


    public static final Object parse(String text);

    --方法作用: 将 json 字符串转为 JSONObject 或者 JSONArray 对象;

    --Object com.alibaba.fastjson.JSON.parse(String text);

    --String text->json 字符串;



    copy
    //将json字符串转为JSONObect对象
  • JSONObjectobject=(JSONObject)JSON.parse(json_student);
  • //将json字符串转为JSONArray对象
  • JSONArrayarray=(JSONArray)JSON.parse(json_array);
  • //打印JSONArray对象
  • System.out.println(array);
  • 运行结果 :

    copy



  • 三. JSONObject 相关用法



    1. JSONObject 方法介绍


    相关方法介绍:

    --获取 JSONObject 中元素: 获取 String 就调用jsonObject.getString("key"),获取 boolean 调用jsonObject.getBoolean("key"),同理还可以获取其它类型的数据;

    --向 JSONObject 中添加元素:object.put("action","fuck");

    --向 JSONObject 中添加多个元素: 将一个 Map 集合添加到 JSONObject 中,Map 集合的键必须是 String 类型;

    copy
    //向JSONObject对象中添加多个属性
  • Map<String,String>map=newHashMap<String,String>();
  • map.put("home","American");
  • map.put("school","harvard");
  • object.putAll(map);
  • -- 移除 JSONObject 中的属性 : jsonObject.remove("key") ,参数是 键;

    --获取 JSONObject 中元素个数:jsonObject.size();



    2. 代码示例


    Main() 函数代码:

    copy
    //将JavaBean对象转为JSONObject对象
  • JSONObjectobject=(JSONObject)JSON.toJSON(student);
  • //打印JSONObject对象
  • System.out.println(object.toString());
  • //获取JSONObject对象的age和male值
  • Stringage=object.getString("age");
  • booleanisMale=object.getBoolean("male");
  • System.out.println("age:"+age+",male:"+isMale);
  • //向JSONObject对象中添加单个属性
  • object.put("action","fuck");
  • System.out.println(object.toString());
  • object.putAll(map);
  • System.out.println(object.toJSONString());
  • //移除JSONObject对象中的属性
  • object.remove("action");
  • System.out.println(object);
  • //获取JSONObject中的属性个数
  • System.out.println(object.size());
  • }


  • 执行结果:

    copy
    age:42,male:true
  • {"action":"fuck","age":42,"schoolId":1}
  • {"action":"fuck","home":"American","school":"harvard","schoolId":1}
  • 7



  • 四. 所有相关代码示例



    1. Java Bean 类


    copy
    }


    2. JSON 相关方法 Main 示例


    copy
    packagecn.org.octopus.fastjson;
  • importjava.util.ArrayList;
  • importjava.util.HashMap;
  • importjava.util.List;
  • importjava.util.Map;
  • importcn.org.octopus.fastjson.beans.Student;
  • importcom.alibaba.fastjson.JSON;
  • importcom.alibaba.fastjson.JSONArray;
  • importcom.alibaba.fastjson.JSONObject;
  • classMain{
  • ;
  • /**
  • *JSONArray相关方法
  • */
  • voidJSONArrayMethod(){
  • //将List集合对象转为JSONArray对象
  • JSONArrayarray=(JSONArray)JSON.toJSON(students);
  • System.out.println("List->JSONArray:"+array);
  • //将List集合转化成json字符串
  • Stringjson_array=JSON.toJSONString(students,153); font-weight:bold; background-color:inherit">true);
  • System.out.println("List->jsonStr"+json_array);
  • //json字符串转为List集合
  • List<Student>students1=JSON.parseArray(json_array,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> System.out.println("jsonStr->List:"+students1);
  • //json字符串转JSONArray
  • JSONArrayarray1=JSON.parseArray(json_array);
  • System.out.println("jsonStr->JSONArray:"+array1);
  • *JSONObject相关方法
  • voidJSONObjectMethod(){
  • Studentstudent=//将JavaBean对象转为JSONObject对象
  • JSONObjectobject=(JSONObject)JSON.toJSON(student);
  • //获取JSONObject对象的age和male值
  • Stringage=object.getString("age");
  • booleanisMale=object.getBoolean("male");
  • System.out.println("age:"+age+",male:"+isMale);
  • //向JSONObject对象中添加单个属性
  • object.put("action","fuck");
  • //向JSONObject对象中添加多个属性
  • Map<String,String>();
  • map.put("home","American");
  • map.put("school","harvard");
  • object.putAll(map);
  • System.out.println(object.toJSONString());
  • //移除JSONObject对象中的属性
  • object.remove("action");
  • //获取JSONObject中的属性个数
  • System.out.println(object.size());
  • /**
  • *JSON.parse()方法示例
  • */
  • voidparseMethod(){
  • //将json字符串转为JSONObect对象
  • JSONObjectobject=(JSONObject)JSON.parse(json_student);
  • JSONArrayarray=(JSONArray)JSON.parse(json_array);
  • *JSON.parseObject()方法示例
  • voidparseObjectMethod(){
  • //将json字符串转为Student对象JSON.parseObject(Stringtext,Class<Student>clazz)
  • *JSON.parseArray()方法示例
  • voidparseArrayMethod(){
  • JSONArrayarray=JSON.parseArray(json_array);
  • //将json字符串转为List集合
  • List<Student>students=JSON.parseArray(json_array,153); font-weight:bold; background-color:inherit">class);
  • //打印List集合大小
  • System.out.println("students.size():"+students.size());
  • //遍历List集合中的元素
  • for(Studentstudent:students)
  • *JSON.toJSON()方法示例
  • voidtoJSONMethod(){
  • Studentstudent_s=//将javabean对象转为JSONObject对象
  • JSONObjectobject=(JSONObject)JSON.toJSON(student_s);
  • System.out.println(object+"--打印JSONOBject本身");
  • System.out.println(object.toString()+"--打印JSONOBject.toString()");
  • System.out.println(object.toJSONString()+"--打印JSONOBject.toJSONString()");
  • //再创建一个Student对象
  • Studentstudent_j=//将两个Student对象放到List集合中
  • List<Student>students=newArrayList<Student>();
  • students.add(student_s);
  • students.add(student_j);
  • Stringformat_json=JSON.toJSONString(students,0); background-color:inherit">*JSON.toJSONString示例
  • voidtoJsonStringMethod(){
  • }


  • http://blog.csdn.net/shulianghan/article/details/41011605

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读