转载请出自出处: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]