转载请出自出处:http://eksliang.iteye.com/blog/2175401
一.概述
从结构上看Json,所有的数据(data)最终都可以分解成三种类型:
- 第一种类型是标量(scalar),也就是一个单独的字符串(string)或数字(numbers),比如"ickes"这个字符串。
- 第二种类型是序列(sequence),又叫做数组(array)或列表(List),比如["北京","上海"]。
- 第三种类型是映射(mapping),又叫做散列(hash)或字典(dictionary),比如"name":"ickes"。
Json的规格非常简单,而且Douglas Crockford声称这个规格永远不必升级,因为该规定的都规定了。
- 并列的数据之间用逗号(",")分隔。
- 映射用冒号(":")表示。
- 集合(数组)用方括号("[]")表示。
- 对象用大括号("{}")表示
二、简单转换
实体类User:
package com.ickes.json.daomain; import java.util.Date; public class User { private String id; private String userName; private String userPwd; private Integer age; private Float price; private Date birthday; public User(String id, String userName, String userPwd, Integer age, Float price, Date birthday) { this.id = id; this.userName = userName; this.userPwd = userPwd; this.age = age; this.price = price; this.birthday = birthday; } get和set方法省略........! @Override public String toString() { return "User [id=" + id + ", userName=" + userName + ", userPwd=" + userPwd + ", age=" + age + ", price=" + price + ", birthday=" + birthday + "]"; } }
单个对象之间的转换,参考实例
import java.util.Date; import com.google.gson.Gson; import com.ickes.json.daomain.User; /** * 简单对象之间的转换 * @author Ickes */ public class GsonTest { public static void main(String[] args) { Gson gson = new Gson(); User user = new User("A001", "xl","xl_123",24,12000F,new Date()); //java对象转换为Json字符串 String json=gson.toJson(user); System.out.println("java对象转json字符串:"+json); //Json字符串转换为java对象 User user1 = gson.fromJson(json,User.class); System.out.println("json字符串转java对象:"+user1.toString()); } }
返回结果
java对象转json字符串: {"id":"A001","userName":"xl","userPwd":"xl_123","age":24,"price":12000.0,"birthday":"Jan 13, 2015 1:43:22 PM"} json字符串转java对象: User [id=A001, userName=xl, userPwd=xl_123, age=24, price=12000.0, birthday=Tue Jan 13 13:43:22 CST 2015]
集合之间的转换
package com.ickes.json; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.google.gson.reflect.TypeToken; import com.google.gson.Gson; import com.ickes.json.daomain.User; /** * 集合之间的转换 * @author Ickes * */ public class GsonTest { public static void main(String[] args) { Gson gson = new Gson(); User user1 = new User("A001", "xl","xl_123",24,12000F,new Date()); User user2 = new User("A002", "x2","xl_123",24,13000F,new Date()); User user3 = new User("A003", "x3","xl_123",24,14000F,new Date()); //集合对象转json List<User> users = new ArrayList<User>(); users.add(user1); users.add(user2); users.add(user3); String jsonObj =gson.toJson(users); System.out.println("List集合对象转换为Json字符串:"+jsonObj); //集合字符串转json List<String> strs = new ArrayList<String>(); strs.add("aaaa"); strs.add("bbbb"); strs.add("cccc"); String jsonStr = gson.toJson(strs); System.out.println("List集合字符串转换为Json字符串:"+jsonStr); //json转集合对象 Type typeObj = new TypeToken<List<User>>(){}.getType(); users=gson.fromJson(jsonObj,typeObj); System.out.println("Json字符串转换为List集合对象:"); for (User user : users) { System.out.println(user); } //json转集合字符串 Type typeStr = new TypeToken<List<String>>(){}.getType(); strs=gson.fromJson(jsonStr,typeStr); System.out.println("Json字符串转换为List集合字符串:"+strs); } }
输出结果
List集合对象转换为Json字符串: [{"id":"A001","userName":"xl","userPwd":"xl_123","age":24,"price":12000.0,"birthday":"Jan 13, 2015 1:41:35 PM"}, {"id":"A002","userName":"x2","userPwd":"xl_123","age":24,"price":13000.0,"birthday":"Jan 13, 2015 1:41:35 PM"}, {"id":"A003","userName":"x3","userPwd":"xl_123","age":24,"price":14000.0,"birthday":"Jan 13, 2015 1:41:35 PM"}] List集合字符串转换为Json字符串:["aaaa","bbbb","cccc"] Json字符串转换为List集合对象: User [id=A001, userName=xl, userPwd=xl_123, age=24, price=12000.0, birthday=Tue Jan 13 13:41:35 CST 2015] User [id=A002, userName=x2, userPwd=xl_123, age=24, price=13000.0, birthday=Tue Jan 13 13:41:35 CST 2015] User [id=A003, userName=x3, userPwd=xl_123, age=24, price=14000.0, birthday=Tue Jan 13 13:41:35 CST 2015] Json字符串转换为List集合字符串:[aaaa, bbbb, cccc]
相关推荐
非常详细的json使用方法。包括对象和json之间的相互转换,集合和json之间的相互转换,自己按照节点解析。gson功能真的很强大。
Gson是Google的一个开源项目,可以将Java对象转换成JSON,也可能将JSON转换成Java对象。 Gson里最重要的对象有2个Gson 和 GsonBuilder Gson有2个最基本的方法 1) toJson() – 转换java 对象到JSON 2) from...
android Gson使用实例 注释详细
Gson的基本使用,特点:a、快速、高效b、代码量少、简洁c、面向对象d、数据传递和解析方便
maven项目中gson使用方法.pdfmaven项目中gson使用方法.pdf
介绍:Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. 压缩包里有以下文件: ...
使用gson解析json的一次测试使用
使用Gson解析Json和Json数组,通俗易懂
om.google.code.gson/gson/2.6.2 Gson maven依赖 java 开发包
gson使用.docgson使用.docgson使用.doc
用户列表对象,使用JSONObject jsonStu = JSONObject.fromObject(list); 就会报错,因为对于嵌套类型的json它是解释不出来的,这时就需要用到com.google.gson.Gson.jar了。资源中提供了jar包与代码示例,一句代码...
Gson基本使用,数据容错处理 适配boolean、Boolean、int、Integer、float、Float、double、Double、long、Long、String类型。 还包括服务器返回的restful规范数据。
GSON简介 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。 Gson是Google提供的用来在Java对象和JSON数据之间进行映射的Java类库。可以将一个JSON字符串...
赠送jar包:gson-2.8.0.jar; 赠送原API文档:gson-2.8.0-javadoc.jar; 赠送源代码:gson-2.8.0-sources.jar; 赠送Maven依赖信息文件:gson-2.8.0.pom; 包含翻译后的API文档:gson-2.8.0-javadoc-API文档-中文...
com.google.gson.Gson 2.8.1 2.8.2 jar包 gson,直接解压到maven仓库,或者提取里面的jar包都可以使用
网络上有很多Json解析库,这里我使用Gson来解析Json,Gson有个特点,就是要将数据的键作为变量封装到一个个实体中,如果值为数组的,变量的类型还必须是集合,然后通过Gson.fromJson来传入数据和实体类,再通过实体...
赠送jar包:gson-2.8.7.jar; 赠送原API文档:gson-2.8.7-javadoc.jar; 赠送源代码:gson-2.8.7-sources.jar; 赠送Maven依赖信息文件:gson-2.8.7.pom; 包含翻译后的API文档:gson-2.8.7-javadoc-API文档-中文...
Gson解析xml最强大的最方便的工具
包含以下java源文件: com.google.gson.DefaultDateTypeAdapter.class com.google.gson.ExclusionStrategy.class com.google.gson.FieldAttributes.class com.google.gson.FieldNamingPolicy.class ...
gson jar包 gson jar包下载 gson jar包下载 gson jar包下载