Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库。可以将一个 JSON 字符串转成一个 Java 对象,或者反过来。

示例代码如下:

实体定义

package com.wisdombud.gson;

public class SimpleEntity {

	private String testInfo;

	private Boolean result;

	public String getTestInfo() {
return testInfo;
} public void setTestInfo(String testInfo) {
this.testInfo = testInfo;
} public Boolean getResult() {
return result;
} public void setResult(Boolean result) {
this.result = result;
}
}

package com.wisdombud.gson; import java.util.List; public class ComplexEntity {
private List<SimpleEntity> list; private String name; private int age; public List<SimpleEntity> getList() {
return list;
} public void setList(List<SimpleEntity> list) {
this.list = list;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
}

核心代码

package com.wisdombud.gson;

import java.util.ArrayList;
import java.util.List; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; public class App {
public static void main(String[] args) {
String s = test2Json();
System.out.println(s);
testFromJson();
} private static String test2Json() {
Gson g = new Gson();
List<ComplexEntity> cList = new ArrayList<ComplexEntity>(); ComplexEntity entity = new ComplexEntity();
cList.add(entity);
entity.setAge(10);
entity.setName("name");
List<SimpleEntity> list = new ArrayList<SimpleEntity>();
entity.setList(list);
SimpleEntity simple = new SimpleEntity();
simple.setResult(true);
simple.setTestInfo("testinfo");
list.add(simple); String s = g.toJson(cList); return s;
} public static void testFromJson() { String json = test2Json();
Gson g = new Gson();
List<SimpleEntity> list = g.fromJson(json,
new TypeToken<List<SimpleEntity>>() {
}.getType()); System.out.println(list.size());
}
}

使用gson进行json转换的更多相关文章

  1. [转]Json转换神器之Google Gson的使用

    这几天,因为项目的需要,接触了Google的Gson库,发现这个东西很好用,遂记下简单的笔记,供以后参考.至于Gson是干什么的,有什么优点,请各位同学自行百度.话不多说,切入正题: 1. 下载Gso ...

  2. Json转换利器Gson之实例一-简单对象转化和带泛型的List转化 (转)

    Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库.可以将一个 JSON 字符串转成一个 Java 对象,或者反过来. jar和源码下载地址: h ...

  3. 从零开始学android开发-Json转换利器Gson之实例

    Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库.可以将一个 JSON 字符串转成一个 Java 对象,或者反过来. jar和源码下载地址: h ...

  4. Gson把json串转换成java实体对象

    Gson把json串转换成java实体对象的方法如下: 1.首先导入Gson的jar包,网上可以下载. java实体对象如下: public class Model { private double ...

  5. Gson解析json字符串、json数组转换成对象

    实体类: public class Product { private int id; private String name; private String date; public int get ...

  6. Json转换工具类(基于google的Gson和阿里的fastjson)

    在项目之中我们经常会涉及到字符串和各种对象的转换,为此特地整理了一下常用的转换方法 一.基于com.google.code.gson封装的json转换工具类 1. 在pom.xml文件里面引入gson ...

  7. Gson将字符串转换成JsonObject和JsonArray

    以下均利用Gson来处理: 1.将bean转换成Json字符串: public static String beanToJSONString(Object bean) { return new Gso ...

  8. Google Gson解析Json数据应用实例

    转自:http://lixigao449778967.blog.163.com/blog/static/24985164201269105928783/ 1.需要的Jar包 1) Google Gso ...

  9. json处理三部曲之第三曲:利用Gson处理json

    需要导入gson-xxx.jar包 <dependency> <groupId>com.google.code.gson</groupId> <artifac ...

随机推荐

  1. Eclipse中Python插件PyDev的安装与配置流程

    安装PyDev插件的两种安装方法: 方法1.下载地址:http://sourceforge.net/projects/pydev/files/,将下载的PyDev解压(目前最新版本 PyDev 4.5 ...

  2. Web内容管理系统 Magnolia 介绍-挖掘优良的架构(1)

    Magnolia简介: Magnolia CMS是一家瑞士公司自2003年起发布的一个基于Java的开源内容管理系统.它适合且已被使用在以下领域:电子商务(例如:COOP.Migros.Rossman ...

  3. 函数buf_page_create

    /********************************************************************//** Initializes a page to the ...

  4. WebService教程和分析

    1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...

  5. VirtualBox的工作原理&参考网上文章

    事先申明,我这里有好多东西都是看网上的,文末给出参考博客链接. 1.在设置里面为什么要选择桥接网络?baidu之后,了解到是虚拟机工作原理的不同,也就是说有好几种工作模式. bridged(桥接模式) ...

  6. COCOS2D-X学习笔记(一)-----Node类的学习

    Node类(在3.0版本以下叫CCNode):节点类. 本文记录以下几个方法的学习笔记: init()和onEnter()这俩个方法都是CCNode的方法.其区别如下: 1.其被调用的顺序是先init ...

  7. 【转】【玩转cocos2d-x之二十三】多线程和同步03-图片异步加载

    原创作品,转载请标明:http://blog.csdn.net/jackystudio/article/details/15334159 cocos2d-x中和Android,Windows都 一样, ...

  8. SPFile的使用

    转:http://blog.csdn.net/pclzr/article/details/7591741 SPFile对应于SharePoint对象模型中的文件,它的使用方法与SPFolder类大致相 ...

  9. java解析XML四种方法

    XML现在已经成为一种通用的数据交换格式,平台的无关性使得很多场合都需要用到XML. XML现在已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便 ...

  10. WebForm+Web.config: 超时时间已到。在操作完成之前超时时间已过或服务器未响应。

    ylbtech-Error-WebForm+Web.config: 超时时间已到.在操作完成之前超时时间已过或服务器未响应. 超时时间已到.在操作完成之前超时时间已过或服务器未响应. 1.A,错误代码 ...