网站建设怎么提需求,淘宝上做网站,wordpress电子邮件要用什么,上海传媒公司名字JSON的处理#xff1a; JSON(JavaScript Object Notation)#xff1a;是一种轻量级的数据交换格式。 它是基于 ECMAScript 规范的一个子集#xff0c;采用完全独立于编程语言的文本格式来存储和表示数据。 简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。易于人阅…JSON的处理 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。 它是基于 ECMAScript 规范的一个子集采用完全独立于编程语言的文本格式来存储和表示数据。 简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。易于人阅读和编写同时也易于计算机解析和生成并有效的 提升网络传输效率。
创建格式 JSON转换工具的介绍 我们除了可以在 JavaScript 中来使用 JSON 以外在 JAVA 中同样也可以使用 JSON。 JSON 的转换工具是通过 JAVA 封装好的一些 JAR 工具包。 可以将 JAVA 对象或集合转换成 JSON 格式的字符串也可以将 JSON 格式的字符串转成 JAVA 对象。 Jackson开源免费的 JSON 转换工具SpringMVC 转换默认使用 Jackson。 导入 jar 包。创建核心对象。调用方法完成转换。
常用方法
常用类 ObjectMapper常用方法 JSON转换
对象转 JSON, JSON 转对象
public class ObjectMapperTest {private ObjectMapper mapper new ObjectMapper();Testpublic void test01() throws Exception {User user new User(韩信, 99);//User对象转jsonString json mapper.writeValueAsString(user);System.out.println(json字符串 json);//json转User对象User user2 mapper.readValue(json, User.class);System.out.println(json对象 user2);}
}Map转 JSON, JSON 转 Map
private ObjectMapper mapper new ObjectMapper(); Test
public void test02() throws Exception {// map转jsonHashMapString, String map new HashMap();map.put(姓名, 韩信);map.put(性别, 男);String json mapper.writeValueAsString(map);System.out.println(json字符串 json);// json转mapHashMap user mapper.readValue(json, HashMap.class);System.out.println(json对象 user);}Test
public void test03() throws Exception {// map转jsonHashMapString, User map new HashMap();map.put(打野, new User(韩信, 99));map.put(中单, new User(不知去向, 97));String json mapper.writeValueAsString(map);System.out.println(json字符串 json);// json转map map如果有自定义类型就要用TypeReferenceHashMapString, User user mapper.readValue(json, new TypeReferenceHashMapString, User() {});System.out.println(json对象 user);}List转 JSON, JSON 转 List Testpublic void test04() throws Exception {// ListString转jsonArrayListString list new ArrayList();list.add(上官原地);list.add(不知去向);String json mapper.writeValueAsString(list);System.out.println(json字符串 json);// json转ListstringArrayList user mapper.readValue(json, ArrayList.class);System.out.println(json对象 user);}Testpublic void test05() throws Exception {// ListString转jsonArrayListUser list new ArrayList();list.add(new User(上官原地, 99));list.add(new User(不知去向, 11));String json mapper.writeValueAsString(list);System.out.println(json字符串 json);// json转ListstringArrayList user mapper.readValue(json, new TypeReferenceArrayListUser() {});System.out.println(json对象 user);}