当前位置:   article > 正文

JSONObject详解(com.alibaba)-fastjson_com.alibaba.fastjson.jsonobject

com.alibaba.fastjson.jsonobject

JSONObject只是一种数据结构,可以理解为JSON格式的数据结构(key-value 结构),可以使用put方法给json对象添加元素。JSONObject可以很方便的转换成字符串,也可以很方便的把其他对象转换成JSONObject对象。

pom(本文所有代码仅使用这一个依赖即可):

<dependencies>

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>fastjson</artifactId>

<version>2.0.12</version>

</dependency>

</dependencies>

1.通过原生生成json数据格式。

  1. import com.alibaba.fastjson.JSONException;
  2. import com.alibaba.fastjson.JSONObject;
  3. public class Demo {
  4. public static void main(String[] args) {
  5. JSONObject zhangsan = new JSONObject();
  6. try {
  7. //添加
  8. zhangsan.put("name", "张三");
  9. zhangsan.put("age", 18.4);
  10. zhangsan.put("birthday", "1900-20-03");
  11. zhangsan.put("majar", new String[] {"哈哈","嘿嘿"});
  12. zhangsan.put("null", null);
  13. zhangsan.put("house", false);
  14. System.out.println(zhangsan.toString());
  15. } catch (JSONException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. }

2.通过hashMap数据结构生成

  1. import com.alibaba.fastjson.JSONObject;
  2. import java.util.HashMap;
  3. public class Demo {
  4. public static void main(String[] args) {
  5. HashMap<String, Object> zhangsan = new HashMap<>();
  6. zhangsan.put("name", "张三");
  7. zhangsan.put("age", 18.4);
  8. zhangsan.put("birthday", "1900-20-03");
  9. zhangsan.put("majar", new String[] {"哈哈","嘿嘿"});
  10. zhangsan.put("null", null);
  11. zhangsan.put("house", false);
  12. System.out.println(new JSONObject(zhangsan).toString());
  13. }
  14. }

3.通过实体生成

需要自定定义实体(例子中实体名为Student),目前本人水平有限,不清楚,日后有机会再完善。

  1. import com.alibaba.fastjson.JSONObject;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. Student student = new Student();
  5. student.setId(1);
  6. student.setAge("20");
  7. student.setName("张三");
  8. //生成json格式
  9. System.out.println(JSON.toJSON(student));
  10. //对象转成string
  11. String stuString = JSONObject.toJSONString(student);
  12. }
  13. }

4.JSON字符串转换成JSON对象

  1. import com.alibaba.fastjson.JSONObject;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. String studentString = "{\"id\":1,\"age\":2,\"name\":\"zhang\"}";
  5. //JSON字符串转换成JSON对象
  6. JSONObject jsonObject1 = JSONObject.parseObject(studentString);
  7. System.out.println(jsonObject1);
  8. }
  9. }

5.list对象转listJson

需要自定定义实体(例子中实体名为Student),目前本人水平有限,不清楚,日后有机会再完善。

  1. import com.alibaba.fastjson.JSONObject;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. ArrayList<Student> studentLsit = new ArrayList<>();
  5. Student student1 = new Student();
  6. student1.setId(1);
  7. student1.setAge("20");
  8. student1.setName("asdasdasd");
  9. studentLsit.add(student1);
  10. Student student2 = new Student();
  11. student2.setId(2);
  12. student2.setAge("20");
  13. student2.setName("aaaa:;aaa");
  14. studentLsit.add(student2);
  15. //list转json字符串
  16. String string = JSON.toJSON(studentLsit).toString();
  17. System.out.println(string);
  18. //json字符串转listJson格式
  19. JSONArray jsonArray = JSONObject.parseArray(string);
  20. System.out.println(jsonArray);
  21. }
  22. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/532690
推荐阅读
相关标签
  

闽ICP备14008679号