Jackson 是一个高性能的JSON Java 库,网址参考:
http://wiki.fasterxml.com/JacksonHome
先看看最简单的Simple data binding怎么使用。就是可以将属于Maps,Lists,Strings,Numbers,Nulls的Java对象转换成JSON对象,当然也可以从JSON对象转换成这些类型的对象。
Simple data binding是Data Binding的简单形式,它是建立在Jackson Streaming API基础上的。Data Binding是一个独立的项目,项目主页在
https://github.com/FasterXML/jackson-databind
下面的代码演示了如何将一个JSON格式的字符串转变成Java的LinkedHashMap对象response。response结果将包含两个entry,两个key分别是message和status。
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Map;
/**
* Hello world!
*
*/
public class App {
public static void main( String[] args ) throws IOException {
ObjectMapper mapper = new ObjectMapper(); // create once, reuse
String jsonSource = "{\"message\":\"Login succeeded!\",\"status\":0}";
Map<String, String> response = mapper.readValue(jsonSource, Map.class);
}
}
注意readValue的第二个参数,Map.class会导致Jackson采用默认LinkedHashMap,如果要指定其他类型的Map来减少内存和CPU的消耗,可以指定TreeMap.class.
如何将对象转换成JSON格式字符串呢,参考下面的代码:
String result = mapper.writeValueAsString(response);
关于null,json中用null来表示,可以自动转换成Java对象的null,反之亦然。
Maven工程需要添加Jackson Data Binding库的依赖。
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
分享到:
相关推荐
Data Binding with Windows Forms 2.0: Programming Smart Client Data Applications with .NET By Brian Noyes ............................................... Publisher: Addison Wesley ...
Android Data Binding结合RecyclerView的使用
This book is all about the what and the why of binding to data sources in a Windows Forms application built using Visual Studio 2005. The book goes into great detail in explaining the rationale behind...
These tutorials describe how to map your classes to your tables manually (rather than with an automated tool like SqlMetal) so that you can have support for M:M relationships and data binding against ...
Brian Noyes is a software architect, trainer, writer, and speaker with IDesign (www.idesign.net), a premier .NET architecture and design consulting and training company. He has been developing ...
豆瓣电影搜索 利用Data Binding在RecycleView展示电影列表
Data Binding with Windows Forms 2.0 Programming Smart Client Data Applications with .NET
3.1 Simple Data Binding 用于json和Java Maps, Lists, Strings, Numbers, Booleans and nulls的相互转换。 3.2 Full Data Binding 用于json和Java Bean的相互转换。 下面从使用的角度上比较三种处理方式: ...
Android Data Binding实战-入门篇学习笔记,包含实例代码。 基于 Android Studio平台。
In this chapter, we examine data binding, the ability to associate non-GUI objects that hold and maintain data with the controls that present and receive that data
AndroidDataBindingExample, Android Data Binding 代码实战
In this chapter, we examine data binding, the ability to associate non-GUI objects that hold and maintain data with the controls that present and receive that data
主要介绍了C#应用BindingSource实现数据同步的方法,需要的朋友可以参考下
记一次 Data Binding 在 library module 中遇到的大坑 使用 Data Binding 也有半年多了,从最初的 setVariable,替换 findViewById,到比较高级的双向绑定,自定义 Adapter、Component,查看源码了解编译、运行流程...
Android Data Binding Library 官方文档中文翻译,出自https://blog.csdn.net/jjwwmlp456/article/details/54915981的博客,被我制作成离线版pdf以供保存, Google官方文档地址:...
微软的示例! VB.net的 数据绑定!
Android是一种基于Linux内核(不包含GNU组件)的自由及开放源代码的移动操作系统,主要应用于移动设备,如智能手机和平板电脑。该系统最初由安迪·鲁宾开发,后被Google公司收购并注资,随后与多家硬件制造商、软件...
Approaches and Best Practices in Web Service Style, Data Binding and Validation.pdf
Android数据绑定的一个Demo程序,包括数据绑定和事件绑定