IT评测·应用市场-qidao123.com技术社区

标题: JSON串 [打印本页]

作者: 玛卡巴卡的卡巴卡玛    时间: 2024-11-9 17:37
标题: JSON串
JSON在客户端的使用

JSON 字符串的格式是基于键值对的数据结构,用于表现结构化数据。它遵循严格的语法规则,常用于前后端数据交互。
1. 根本结构


JSON 对象示例:

  1. { "name": "张三", "age": 25, "isStudent": true }
复制代码
JSON 数组示例:

  1. { "subjects": ["Math", "Science", "History"] }
复制代码
2. 数据类型

JSON 支持以下几种数据类型:

示例:

  1. { "name": "张三", "age": 25, "isStudent": true,
  2. "grades": [85, 90, 95], "address": { "city": "北京", "postalCode": "100000" },
  3. "graduationYear": null }
复制代码
3. 语法规则


示例:

  1. { "name": "张三", "age": 25, "isStudent": true,
  2. "subjects": ["Math", "Science", "History"] }
复制代码
4. 嵌套结构

JSON 支持对象和数组的嵌套,这使得它可以或许表达复杂的结构。
示例:

  1. { "name": "张三", "age": 25,
  2. "dog": { "name": "小花", "age": 3 },
  3. "friends": [ { "name": "李四", "age": 24 }, { "name": "王五", "age": 26 } ]
  4. }
复制代码
在这个例子中,dog 是一个嵌套的对象,而 friends 是一个包含多个对象的数组。
将JSON串转为对象以及把对象转为JSON串的例子

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Title</title>
  6.     <script>
  7.         // 定义一个符合JSON格式的字符串
  8.         var personStr = '{ "name": "张三", "age": 10,
  9.         "dog": { "dname": "小花" }, "loveSingers": ["张小明", "王小东", "李小黑"],
  10.         "friends": [{"fname": "李四"}, {"fname": "张三"}] }';
  11.         // 通过JSON.parse()可以把一个JSON字符串转换为一个对象
  12.         var person = JSON.parse(personStr);
  13.         // console.log(personStr);
  14.         // console.log(person);
  15.         console.log(person.name);
  16.         console.log(person.age);
  17.         console.log(person.dog.dname);
  18.         console.log(person.loveSingers[0]);
  19.         console.log(person.friends[0].fname);
  20.         // 通过JSON.stringify() 将一个对象转换为JSON串
  21.         var personStr2 = JSON.stringify(person);
  22.     </script>
  23. </head>
  24. <body>
  25. </body>
  26. </html>
复制代码

JSON在服务器端的使用 

1. 将对象转换为 JSON 字符串

这个过程称为 序列化(Serialization),它将 Java 对象转换为 JSON 字符串,以便可以通过 HTTP 传输大概存储到数据库中。
焦点步骤: 


        下载包的地址:
下载后搜刮Jackson
https://mvnrepository.com/


  1. public static void ObjectToString() throws JsonProcessingException {
  2.     // 创建对象
  3.     Dog dog = new Dog("招财");
  4.     Person person = new Person("张三", 10, dog);
  5.     // 将对象转换为 JSON 字符串
  6.     ObjectMapper objectMapper = new ObjectMapper();
  7.     String personStr = objectMapper.writeValueAsString(person);
  8.     // 输出 JSON 字符串
  9.     System.out.println(personStr);
  10. }
复制代码
 输出:
  1. {
  2.   "name": "张三",
  3.   "age": 10,
  4.   "dog": {
  5.     "name": "招财"
  6.   }
  7. }
复制代码
2. 将 JSON 字符串转换为 Java 对象

这个过程称为 反序列化(Deserialization),它将 JSON 字符串转换为 Java 对象,以便在后端进行进一步的业务逻辑处置惩罚。
焦点步骤:


  1. public static void JsonToEObject() throws JsonProcessingException {
  2.     // JSON 字符串
  3.     String personStr = "{"name":"张三","age":10,"dog":{"name":"招财"}}";
  4.     // 将 JSON 字符串转换为对象
  5.     ObjectMapper objectMapper = new ObjectMapper();
  6.     Person person = objectMapper.readValue(personStr, Person.class);
  7.     // 输出对象
  8.     System.out.println(person);
  9. }
复制代码
调式时的控制台: 

3. 总结:如何在服务器端进行 JSON 和对象之间的转换

对象转换为 JSON 字符串(序列化)


JSON 字符串转换为对象(反序列化)



JSON和Map/List/Array之间的转换:

1. JSON 和 Map 之间的转换


1.1 将 JSON 转换为 Map

使用 ObjectMapper 的 readValue() 方法将 JSON 字符串转换为 Map<String, Object>。
  1. import com.fasterxml.jackson.databind.ObjectMapper;
  2. import java.util.Map;
  3. public class JsonToMapExample {
  4.     public static void main(String[] args) throws Exception {
  5.         String jsonStr = "{"name":"张三", "age":30, "dog":{"name":"小花"}}";
  6.         // 创建 ObjectMapper
  7.         ObjectMapper objectMapper = new ObjectMapper();
  8.         // 将 JSON 转换为 Map
  9.         Map<String, Object> resultMap = objectMapper.readValue(jsonStr, Map.class);
  10.         
  11.         // 输出 Map 内容
  12.         System.out.println(resultMap);
  13.     }
  14. }
复制代码
1.2 将 Map 转换为 JSON
  1. public  static void MapToJson() throws JsonProcessingException {
  2.         Map data = new HashMap();
  3.         data.put("a","value");
  4.         data.put("b","value");
  5.         ObjectMapper objectMapper = new ObjectMapper();
  6.         String s = objectMapper.writeValueAsString(data);
  7.         System.out.println(s);
  8.     }
复制代码
2. JSON 和 List 之间的转换


2.1 将 JSON 数组转换为 List

JSON 数组可以直接转换为 List,可以是 List<String>、List<Integer> 或 List<Map<String, Object>> 等。
  1. import com.fasterxml.jackson.core.type.TypeReference;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import java.util.List;
  4. public class JsonToListExample {
  5.     public static void main(String[] args) throws Exception {
  6.         String jsonArrayStr = "["张三", "李四", "王五"]";
  7.         // 创建 ObjectMapper
  8.         ObjectMapper objectMapper = new ObjectMapper();
  9.         // 将 JSON 数组转换为 List
  10.         List<String> resultList = objectMapper.readValue(jsonArrayStr, new TypeReference<List<String>>() {});
  11.         // 输出 List 内容
  12.         System.out.println(resultList);
  13.     }
  14. }
复制代码
2.2 将 List 转换为 JSON 数组

使用 writeValueAsString() 方法可以将 List 转换为 JSON 数组。
  1.    public  static void ListToJson() throws JsonProcessingException {
  2. //        List<String> data1 = new ArrayList<String>();
  3. //        data1.add("a");
  4. //        data1.add("b");
  5. //        data1.add("c");
  6. //
  7. //        String [] data2 = {"a","b","c"};
  8.         Dog dog = new Dog("招财");
  9.         Person person = new Person("张三",10,dog);
  10.         List<Person> data = new ArrayList<Person>();
  11.         data.add(person);
  12.         ObjectMapper objectMapper = new ObjectMapper();
  13.         String s = objectMapper.writeValueAsString(data);
  14.         System.out.println(s);
  15.     }
复制代码





免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4