原文地址: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. 未能加载文件或程序集“Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。

    未能加载文件或程序集“Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed”或它的某一个 ...

  2. html5 canvas图片反色

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. TDBXCommand TDBXReader

    TDBXCommand  *cmd; cmd= FDBXConnection->CreateCommand(); cmd->CommandType=TDBXCommandTypes_DSS ...

  4. UNITY3D MAC版本破解

    百度网盘下载地址: http://pan.baidu.com/s/1eQmvLqa#path=%252F 包含本体和破解文件 首先说明一下,如果是公司做开发建议去购买正版. 之前网上也有很多人贴出了破 ...

  5. codeforces 601A The Two Routes(最短路 flody)

    A. The Two Routes time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  6. python用httplib模块发送get和post请求

    在python中,模拟http客户端发送get和post请求,主要用httplib模块的功能. 1.python发送GET请求 我在本地建立一个测试环境,test.php的内容就是输出一句话: 1 e ...

  7. UI进阶 文件管理器(NSFileManager)文件对接器(NSFileHandle)

    一.文件管理器与文件连接器之间的区别 文件管理器(NSFileManager) 此类主要是对文件进行的操作(创建/删除/改名等)以及文件信息的获取. 文件连接器(NSFileHandle) 此类主要是 ...

  8. android ExpandAbleListView控件

    ExpandAbleListView控件 1.API对ExpandAbleListView的解释:

  9. 使用Canvas把照片转换成素描画

    原文:http://www.alloyteam.com/2012/07/convert-picture-to-sketch-by-canvas/ 腾讯的alloy team写的一个素描效果,挺不错的. ...

  10. MFC 构建、消亡 顺序 (一)--单文档 (SDI)

    MFC 构建.消亡 顺序 (一)--单文档 (SDI) by:http://www.cnblogs.com/vranger/ (一)SDI 生成顺序 (二)打开文档-“Open” (三)新建文档-“N ...