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. 函数buf_page_get_gen

    /********************************************************************//** This is the general functi ...

  2. this class is not key value coding-compliant for the key ##

    setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key ## 出现以上错误时很恶心,并 ...

  3. qt创建android项目后需要加入的参数

    默认用qtcreator5.2.0创建了一个quick项目,却报如下错误: error:cstdlib.h no such file or directory 解决方法: 打开项目文件untitled ...

  4. error LNK2019: 无法解析的外部符号 __imp___CrtDbgReportW

    error LNK2005 and error LNK2019 error LNK2019: unresolved external symbol __imp___CrtDbgReportW refe ...

  5. How to modify squashfs image

    /********************************************************************** * How to modify squashfs ima ...

  6. 通过Instant Client包来使用SQL*PLUS

    1.首先下载两个程序包: Instant Client Package - Basic(或Instant Client Package - Basic Lite)包 Instant Client Pa ...

  7. 利用c#反射实现实体类生成以及数据获取与赋值

    转:http://hi.baidu.com/xyd21c/item/391da2fc8fb351c10dd1c8b8 原有的实体类成员逐个赋值与获取的方法弊端: 1.每次对实体类属性进行赋值时,都要检 ...

  8. Nginx源码安装及调优配置

    导读 由于Nginx本身的一些优点,轻量,开源,易用,越来越多的公司使用nginx作为自己公司的web应用服务器,本文详细介绍nginx源码安装的同时并对nginx进行优化配置. Nginx编译前的优 ...

  9. 动态 SQL

    MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格,还要注意省掉 ...

  10. Delphi 712操作word

    //导出Wordprocedure TFrm_Computing.ExportWord;var wordApp, WordDoc, WrdSelection, wrdtable, wrdtable1, ...