当前位置:   article > 正文

JSONObject的各种用法

jsonobject
     //1.java对象转化成String
     String s=JSONObject.toJSONString(javaObject.class);

     //2. java对象转化成Object
        Object str=JSONObject.toJSON(javaObject.class);

     //3.String类型转json对象
       JSONObject jsonObject= JSONObject.parseObject(str);

     //4. String转Object
        Object obj=JSONObject.parse(str);

     //5.  json对象转化成java对象
       Student stu=JSONObject.toJavaObject(jsonObject, Student.class);

     //6. String转化为Map类型
        Map map = JSONObject.parseObject(str,Map.class);

     // 7.String类型的集合转List<String>
        List<String> list= JSONObject.parseArray(stringList,String.class);

      //8.  json对象转化为List类型
       List list=JSONArray.parseArray(JSONObject.toJSONString(jsonObject, List.class))

      //9.String转jsonArray
        JSONArray jsaonArray=JSONObject.parseArray(str);
        
      //10.String转java对象
       Student stu=JSON.parseObject(str,Student.class);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

举例:

1.Json结构的字符串转java对象类型的List
  JSONObject jsonObj= JSONObject.parseObject(str);
  //如果转化后的json对象包含集合
  JSONArray jsonArray= jsonObj.getJSONArray("studentList");
  // List<Student> stuList=   	   JSONObject.parseArray(jsonArray.toJSONString(),Student.class);
  List<Student> stuList= jsonArray.toJavaList(Student.class);

2.obj转List<Map>
 List<Map> models = JSONObject.parseArray(JSON.toJSONString(obj), Map.class);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/503608
推荐阅读
相关标签
  

闽ICP备14008679号