JSON

Jackson学习二之集合类对象与JSON互相转化

字号+ 作者:H5之家 来源:H5之家 2018-01-19 14:02 我要评论( )

本篇主要演示如何使用Jackson对List, Map和数组与JSON互相转换. package com.jingshou.jackson; import java.io.IOException; import java.util.ArrayList; imp

本篇主要演示如何使用Jackson对List, Map和数组与JSON互相转换.

package com.jingshou.jackson; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import com.fasterxml.jackson.databind.ObjectMapper; import com.jingshou.pojo.Student; public class JacksonTest2 { public static void main(String[] args) throws IOException { Student student1 = new Student(); student1.setId(5237); student1.setName("jingshou"); student1.setBirthDay(new Date()); Student student3 = new Student(); student3.setId(5117); student3.setName("saiya"); student3.setBirthDay(new Date()); ObjectMapper mapper = new ObjectMapper(); //Convert between List and JSON List<Student> stuList = new ArrayList<Student>(); stuList.add(student1); stuList.add(student3); String jsonfromList = mapper.writeValueAsString(stuList); System.out.println(jsonfromList); //List Type is not required here. List stuList2 = mapper.readValue(jsonfromList, List.class); System.out.println(stuList2); System.out.println("************************************"); //Convert Map to JSON Map<String, Object> map = new HashMap<String, Object>(); map.put("studentList", stuList); map.put("class", "ClassName"); String jsonfromMap = mapper.writeValueAsString(map); System.out.println(jsonfromMap); Map map2 = mapper.readValue(jsonfromMap, Map.class); System.out.println(map2); System.out.println(map2.get("studentList")); System.out.println("************************************"); //Convert Array to JSON Student[] stuArr = {student1, student3}; String jsonfromArr = mapper.writeValueAsString(stuArr); System.out.println(jsonfromArr); Student[] stuArr2 = mapper.readValue(jsonfromArr, Student[].class); System.out.println(Arrays.toString(stuArr2)); } }

运行结果:

[{"id":5237,"name":"jingshou","birthDay":1389528275987},{"id":5117,"name":"saiya","birthDay":1389528275987}] [{id=5237, name=jingshou, birthDay=1389528275987}, {id=5117, name=saiya, birthDay=1389528275987}] ************************************ {"class":"ClassName","studentList":[{"id":5237,"name":"jingshou","birthDay":1389528275987},{"id":5117,"name":"saiya","birthDay":1389528275987}]} {class=ClassName, studentList=[{id=5237, name=jingshou, birthDay=1389528275987}, {id=5117, name=saiya, birthDay=1389528275987}]} [{id=5237, name=jingshou, birthDay=1389528275987}, {id=5117, name=saiya, birthDay=1389528275987}] ************************************ [{"id":5237,"name":"jingshou","birthDay":1389528275987},{"id":5117,"name":"saiya","birthDay":1389528275987}] [Student [birthDay=Sun Jan 12 20:04:35 CST 2014, id=5237, name=jingshou], Student [birthDay=Sun Jan 12 20:04:35 CST 2014, id=5117, name=saiya]]

再举一例实际应用:

小米网站注册页面输入邮件地址后,服务器提交的Ajax请求是:

https://account.xiaomi.com/pass/user@externalIdBinded?externalId=9999999%40qq.com&type=EM

服务器的返回是:&&&START&&&{"result":"ok","description":"成功","data":{"userId":-1},"code":0}

我们可以尝试用Map去读取后面那一段JSON

package com.jingshou.jackson; import java.io.IOException; import java.util.Map; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonTest3 { public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException { String json = "{\"result\":\"ok\",\"description\":\"成功\",\"data\":{\"userId\":-1},\"code\":0}"; ObjectMapper mapper = new ObjectMapper(); Map map = mapper.readValue(json, Map.class); //输出 {result=ok, description=成功, data={userId=-1}, code=0} System.out.println(map); //输出{userId=-1} Map dataMap = (Map) map.get("data"); System.out.println(dataMap); } }

可见以Key-Value形式的JSON字符串,都可以直接使用Map成功读取出来

本文出自"lijingshou"博客,转载请务必保留此出处

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Jackson学习一之对象与JSON互相转化

    Jackson学习一之对象与JSON互相转化

    2018-01-18 18:03

  • Jackson将json string转为Object,org.json读取json数组

    Jackson将json string转为Object,org.json读取json数组

    2017-12-16 11:00

  • 【学习】spring MVC之返回JSON数据(Spring3.0 MVC+Jackson+AJAX)

    【学习】spring MVC之返回JSON数据(Spring3.0 MVC+Jackson+AJAX)

    2017-12-10 12:01

  • Jackson 通过自定义注解来控制json key的格式

    Jackson 通过自定义注解来控制json key的格式

    2017-12-06 10:05

网友点评