欢迎光临Software MyZone,有问题可留言或到站点论坛发帖,争取第一时间帮忙解决 || 站点论坛:火龙论坛 || 淘宝小店:应小心的易淘屋 【欢迎大家提建设性意见】

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

欢迎热爱编程的朋友们参与到cocos2d-x编程中,为了给大家提供良好的交流环境,网站以开启QQ群
Software MyZone:66202765(群号,欢迎加入,若满,请加1群)
Software MyZone 1群(2dx):286504621
Software MyZone 2群(山东):204954191
Software MyZone【Java群】:162865493
【加群请写:Software MyZone或者是firedragonpzy】
淘宝店:【应小心的易淘屋】初次开店,大家多多支持……
群论坛:【火龙论坛】正试运营阶段,欢迎大家多提些建设性意见……
群问答:【火龙问答】正试运营阶段,欢迎大家多提些建设性意见……
摘自:http://blog.csdn.net/lk_blog/article/details/7685169

我的digitalocean推广链接:https://www.digitalocean.com/?refcode=65dfdb158f1a,Everyone you refer gets $10 in credit,从此链接注册,你将获得10美元。

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

jar和源码下载地址: http://code.google.com/p/google-gson/downloads/list
实体类:

public class Student {
private int id;
private String name;
private Date birthDay;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Date getBirthDay() {
return birthDay;
}

public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}

@Override
public String toString() {
return "Student [birthDay=" + birthDay + ", id=" + id + ", name="
+ name + "]";
}

}

测试类:

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class GsonTest1 {

public static void main(String[] args) {
Gson gson = new Gson();

Student student1 = new Student();
student1.setId(1);
student1.setName("李坤");
student1.setBirthDay(new Date());

// //////////////////////////////////////////////////////////
System.out.println("----------简单对象之间的转化-------------");
// 简单的bean转为json
String s1 = gson.toJson(student1);
System.out.println("简单Bean转化为Json===" + s1);

// json转为简单Bean
Student student = gson.fromJson(s1, Student.class);
System.out.println("Json转为简单Bean===" + student);
// 结果:
// 简单Bean转化为Json==={"id":1,"name":"李坤","birthDay":"Jun 22, 2012 8:27:52 AM"}
// Json转为简单Bean===Student [birthDay=Fri Jun 22 08:27:52 CST 2012, id=1,
// name=李坤]
// //////////////////////////////////////////////////////////

Student student2 = new Student();
student2.setId(2);
student2.setName("曹贵生");
student2.setBirthDay(new Date());

Student student3 = new Student();
student3.setId(3);
student3.setName("柳波");
student3.setBirthDay(new Date());

Listlist = new ArrayList();
list.add(student1);
list.add(student2);
list.add(student3);

System.out.println("----------带泛型的List之间的转化-------------");
// 带泛型的list转化为json
String s2 = gson.toJson(list);
System.out.println("带泛型的list转化为json==" + s2);

// json转为带泛型的list
List retList = gson.fromJson(s2,
new TypeToken >() {
}.getType());
for (Student stu : retList) {
System.out.println(stu);
}

// 结果:
// 带泛型的list转化为json==[{"id":1,"name":"李坤","birthDay":"Jun 22, 2012 8:28:52 AM"},{"id":2,"name":"曹贵生","birthDay":"Jun 22, 2012 8:28:52 AM"},{"id":3,"name":"柳波","birthDay":"Jun 22, 2012 8:28:52 AM"}]
// Student [birthDay=Fri Jun 22 08:28:52 CST 2012, id=1, name=李坤]
// Student [birthDay=Fri Jun 22 08:28:52 CST 2012, id=2, name=曹贵生]
// Student [birthDay=Fri Jun 22 08:28:52 CST 2012, id=3, name=柳波]

}
}

执行结果:

//———-简单对象之间的转化————-
//简单Bean转化为Json==={“id”:1,”name”:”李坤”,”birthDay”:”Jun 22, 2012 9:10:31 PM”}
//Json转为简单Bean===Student [birthDay=Fri Jun 22 21:10:31 CST 2012, id=1, name=李坤]
//———-带泛型的List之间的转化————-
//带泛型的list转化为json==[{"id":1,"name":"李坤","birthDay":"Jun 22, 2012 9:10:31 PM"},{"id":2,"name":"曹贵生","birthDay":"Jun 22, 2012 9:10:31 PM"},{"id":3,"name":"柳波","birthDay":"Jun 22, 2012 9:10:31 PM"}]
Student [birthDay=Fri Jun 22 21:10:31 CST 2012, id=1, name=李坤]
Student [birthDay=Fri Jun 22 21:10:31 CST 2012, id=2, name=曹贵生]
Student [birthDay=Fri Jun 22 21:10:31 CST 2012, id=3, name=柳波]

  1. Json转换利器Gson之实例一-简单对象转化和带泛型的List转化
  2. Json转换利器Gson之实例二-Gson注解和GsonBuilder
  3. Json转换利器Gson之实例三-Map处理(上)
  4. Json转换利器Gson之实例四-Map处理(下)
  5. Json转换利器Gson之实例五-实际开发中的特殊需求处理
  6. Json转换利器Gson之实例六-注册TypeAdapter及处理Enum类型
Tags: ,

发表评论