原文地址: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. 【转】Mysql进程管理

    mysql> show processlist; +----+------+-----------+------+---------+------+-------+--------------- ...

  2. <系统函数实现>memcmp

    这是我实现的memcmp函数: #include <stdio.h> #include <string.h> /* *int memcmp (const void *s1,co ...

  3. Android NDK调试C++源码(转)

    [原创文章,转载请保留或注明出处,http://download.csdn.net/download/bigmaxim/5474055] 1. 相关软件 adt-bundle-windows-x86. ...

  4. XML的特殊字符处理

    XML中共有5个特殊的字符,分别是:&<>“’.如果配置文件中的注入值包括这些特殊字符,就需要进行特别处理.有两种解决方法:其一,采用本例中的<![CDATA[ ]]> ...

  5. Dell商用台式机、笔记本、服务器800电话

    戴尔Optiplex商用台式机 售后服务电话 800-858-0950 选1选2选2 戴尔Latitude商用笔记本 售后服务电话 800-858-0950 选1选3选2 戴尔服务器PowerEdge ...

  6. CodeForces 709C Letters Cyclic Shift (水题)

    题意:给定一个字符串,让你把它的一个子串字符都减1,使得总字符串字典序最小. 析:由于这个题是必须要有一个字串,所以你就要注意这个只有一个字符a的情况,其他的就从开始减 1,如果碰到a了就不减了,如果 ...

  7. android打电话、发短信实现

    打电话: Intent intent = newIntent(Intent.ACTION_CALL,Uri.parse("tel:"+"156666666666" ...

  8. css 关于两栏布局,左边固定,右边自适应

    好几个星期都没写博客了,最近不忙也不闲,稀里糊涂过了两个星期,之前几个月内天天坚持签到.最近也没签到.哈哈,说正事. 今天做东钿互金平台后台页面,昨天做了一个登录页面,业偶碰到了一个难题.等下也要把它 ...

  9. setTimeout setInterval 区别 javascript线程解释

    原文:http://www.iamued.com/qianduan/1645.html 今天看到这篇文章,学到了不少东西 特此发出来 和大家分享 JavaScript的setTimeout与setIn ...

  10. 消息系统Kafka介绍 - 董的博客

    1.  概述 Kafka是Linkedin于2010年12月份开源的消息系统,它主要用于处理活跃的流式数据.活跃的流式数据在web网站应用中非常常见,这些数据包括网站的pv.用户访问了什么内容,搜索了 ...