原文地址: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. gradle使用eclipse debug 代码

    设置环境变量 unix,linux      export GRADLE_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=9999,s ...

  2. Windows Azure 虚拟网络配置(Point to Site)

    说明:本文以Azure国际版为例,中国版在网络位置会存在一定差异. 1. 场景 虚拟网络为我们提供了在Windows Azure云计算环境上构建网络定义的能力,通过虚拟网络,我们可以方便地将Windo ...

  3. android学习笔记---发送状态栏通知

    发送消息的代码如下: //获取通知管理器 NotificationManager mNotificationManager = (NotificationManager) getSystemServi ...

  4. windows下python的包管理器pip安装

    pip:  A tool for installing and managing Python packages. 要用到第三方包,python的版本就必须是2.7 https://pypi.pyth ...

  5. Ubuntu 创建开机自启动脚本的方法

    http://askubuntu.com/questions/9382/how-can-i-configure-a-service-to-run-at-startuphttp://stackoverf ...

  6. Javascript函数柯里化(curry)

    函数柯里化currying,是函数式编程非常重要的一个标志.它的实现需要满足以下条件,首先就是函数可以作为参数进行传递,然后就是函数可以作为返回值return出去.我们依靠这个特性编写很多优雅酷炫的代 ...

  7. C++11角括号

    [C++11角括号] 标准 C++ 的剖析器一律将 ">>" 视为右移运算符. 但在样板定义式中,绝大多数的场合其实都代表两个连续右角括号. 为了避免剖析器误判,撰码时 ...

  8. [原创]Devexpress XtraReports 系列 5 创建交叉报表

    昨天我们已经介绍了如何创建多栏报表,详见:[原创]Devexpress XtraReports 系列 4 创建多栏报表 今天我们继续我们的XtraReports系列.Demo和数据库文件最后会附上. ...

  9. Chocolatey的安装与使用

    @(编程) 前言 在 Linux 下,大家喜欢用 apt-get 来安装应用程序,如今在 windows 下,大家可以使用 Chocolatey 来快速下载搭建一个开发环境. Chocolatey 的 ...

  10. 接口是干爹, 继承是亲爹 ---JAVA

    接口(interface)是干爹, 因为你可以有很多很多的干爹爹... 继承(extends)是亲爹, 因为你只能有一个父类, 只有一个亲生的父亲. 单继承,多接口?./>./..