原文地址:http://lijingshou.iteye.com/blog/2003059

本篇主要演示如何使用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);
} }

Jackson学习二之集合类对象与JSON互相转化--转载的更多相关文章

  1. FastJson、Jackson、Gson进行Java对象转换Json细节处理

    前言 Java对象在转json的时候,如果对象里面有属性值为null的话,那么在json序列化的时候要不要序列出来呢?对比以下json转换方式 一.fastJson 1.fastJson在转换java ...

  2. FastJson、Jackson、Gson进行Java对象转换Json的细节处理

    前言 Java对象在转json的时候,如果对象里面有属性值为null的话,那么在json序列化的时候要不要序列出来呢?对比以下json转换方式 一.fastJson 1.fastJson在转换java ...

  3. js 对象与json的转化

    1.将对象转换为JSON格式字符串 JSON.stringify(object) 2.将JSON字符串转换为对象 JSON.parse(jsonString);

  4. Python之dict(或对象)与json之间转化

    在Python语言中,json数据与dict字典以及对象之间的转化,是必不可少的操作. 在Python中自带json库.通过import json导入. 在json模块有2个方法, loads():将 ...

  5. Vue源码学习二 ———— Vue原型对象包装

    Vue原型对象的包装 在Vue官网直接通过 script 标签导入的 Vue包是 umd模块的形式.在使用前都通过 new Vue({}).记录一下 Vue构造函数的包装. 在 src/core/in ...

  6. JavaScript之面向对象学习二(原型属性对象与in操作符)获取对象中所有属性的方法

    1.原型属性对象于in操作符之in单独使用 有两种方式使用in操作符:单独使用和在for-in循环中使用.在单独使用中,代码如下: function Person(){ } Person.protot ...

  7. ADO.NET基础学习 二(Command对象)

    ②command对象用来操作数据库.(三个重要的方法:ExecuteNonQuery(),ExecuteReader(),ExecuteScalar()) ⑴以update(改数据)为例,用到Exec ...

  8. vue-router query 传对象需要JSON.stringify()转化

    先说一下场景-微信公众号网页开发中,一个文章列表点击跳转详情页.代码如下 1 2 3 this.$router.push({path: '/wx/detail', query: {res: data} ...

  9. Java对象、Json、Xml转换工具Jackson使用

    在Java项目中將一个对象转换成一段Json格式的字符串是非常常见的,能够实现这种需求的工具包也比较多,例如Gson.JSON-lib.Jackson等等.本文主要介绍Jackson的使用,Jacks ...

随机推荐

  1. HDU-3874 Necklace 线段树+离线

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3874 比较简单的题,题意也好懂. 先O(n)求每个数左边第一次出现的与他相同的数的位置l[i].对询问 ...

  2. ASP.NET MVC - 发布网站

    原文地址:http://www.w3school.com.cn/aspnet/mvc_publish.asp 学习如何在不使用 Visual Web Developer 的情况下发布 MVC 应用程序 ...

  3. mysql innobackupex xtrabackup 大数据量 备份 还原(转)

    原文:http://blog.51yip.com/mysql/1650.html 作者:海底苍鹰 大数据量备份与还原,始终是个难点.当MYSQL超10G,用mysqldump来导出就比较慢了.在这里推 ...

  4. SpringMVC处理Date类型的成员变量方法

    原文链接:http://www.tuicool.com/articles/aYfaqa 在使用 SpringMVC 的时候,我们可能需要将一个对象从 View 传递给 Controller .而当这个 ...

  5. 用LinkedHashMap实现LRU算法

    (在学习操作系统时,要做一份有关LRU和clock算法的实验报告,很多同学都应该是通过数组去实现LRU,可能是对堆栈的使用和链表的使用不是很熟悉吧,在网上查资料时看到了LinkedHashMap,于是 ...

  6. svn's diff command

    [svn's diff command] svn diff 比较的是版本快照, 跟merge的应用diff完全不一样. 缺省情况下,svn diff忽略文件的祖先,只会比较两个文件的内容.如果你使用- ...

  7. LC并联谐振回路

  8. qq邮箱发送

    454 Authentication failed, please open smtp flag first!用QQ邮箱测试报错 我用QQ邮箱测试javamail发送邮件的功能,用户名密码设置正确,却 ...

  9. C#中的结构体与类的区别

    经常听到有朋友在讨论C#中的结构与类有什么区别.正好这几日闲来无事,自己总结一下,希望大家指点. 1. 首先是语法定义上的区别啦,这个就不用多说了.定义类使用关键字class 定义结构使用关键字str ...

  10. 小巧实用js倒计时

    <script type="text/javascript">     var intDiff = parseInt(15); //倒计时总秒数量     functi ...