JSON 互相转化

JSON 互相转化一 JSON 1 JSON 介绍 JSON 是一种纯字符串形式的数据 它本身不提供任何方法 函数 非常适合在网络中进行传输 JavaScript PHP Java Python C 等编程语言中都内置了处理 JSON 数据的方法 JSON 是基于 JavaScript 的一个子集

大家好,我是讯享网,很高兴认识大家。

一. JSON

1.JSON 介绍

JSON 是一种纯字符串形式的数据,它本身不提供任何方法(函数),非常适合在网络中进行传输。JavaScript、PHP、Java、Python、C++ 等编程语言中都内置了处理 JSON 数据的方法。

JSON 是基于 JavaScript的一个子集,是一种开放的、轻量级的数据交换格式,采用独立于编程语言的文本格式来存储和表示数据,易于程序员阅读与编写,同时也易于计算机解析和生成,通常用于在 Web 客户端(浏览器)与 Web 服务器端之间传递数据。

在 JSON 中,使用以下两种方式来表示数据:


讯享网

2. 导入依赖

 <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version></version> </dependency>

讯享网

3. JSON转对象

讯享网Student o = JSONObject.parseObject(jsonString, Student.class); 

4. 对象或者List集合转化为JSON

 List<Student> studentList = new ArrayList<>(); Student deme1 = new Student(); deme1.setDiscipline("0"); deme1.setLongitude(120.); deme1.setLatitude(31.); studentList.add(deme1); String s2 = JSON.toJSONString(deme1);//对象转化为json Student deme2 = new Student(); deme2.setDiscipline("0"); deme2.setLongitude(120.); deme2.setLatitude(31.); studentList.add(deme2); String s = JSON.toJSONString(studentList);//List转换为json

二. JSONObject

1. JSONObject 介绍

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

2.通过原生生成json数据格式

讯享网JSONObject zhangsan = new JSONObject(); try { //添加 zhangsan.put("name", "张三"); zhangsan.put("age", 18.4); zhangsan.put("birthday", "1900-20-03"); zhangsan.put("majar", new String[] {"哈哈","嘿嘿"}); zhangsan.put("null", null); zhangsan.put("house", false); System.out.println(zhangsan.toString()); } catch (JSONException e) { e.printStackTrace(); }

3.通过hashMap数据结构生成

 HashMap<String, Object> zhangsan = new HashMap<>(); zhangsan.put("name", "张三"); zhangsan.put("age", 18.4); zhangsan.put("birthday", "1900-20-03"); zhangsan.put("majar", new String[] {"哈哈","嘿嘿"}); zhangsan.put("null", null); zhangsan.put("house", false); System.out.println(new JSONObject(zhangsan).toString());

4.通过实体生成 

讯享网 Student student = new Student(); student.setId(1); student.setAge("20"); student.setName("张三"); //生成json格式 System.out.println(JSON.toJSON(student)); //对象转成string String stuString = JSONObject.toJSONString(student);

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

String studentString = "{\"id\":1,\"age\":2,\"name\":\"zhang\"}"; //JSON字符串转换成JSON对象 JSONObject jsonObject1 = JSONObject.parseObject(stuString); System.out.println(jsonObject1);

6. list对象转listJson

讯享网ArrayList<Student> studentLsit = new ArrayList<>(); Student student1 = new Student(); student1.setId(1); student1.setAge("20"); student1.setName("asdasdasd"); studentLsit.add(student1); Student student2 = new Student(); student2.setId(2); student2.setAge("20"); student2.setName("aaaa:;aaa"); studentLsit.add(student2); //list转json字符串 String string = JSON.toJSON(studentLsit).toString(); System.out.println(string); //json字符串转listJson格式 JSONArray jsonArray = JSONObject.parseArray(string); System.out.println(jsonArray);
小讯
上一篇 2025-03-04 11:14
下一篇 2025-01-25 11:10

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/17578.html