首页 > 其他 > 详细

Gson简要使用

时间:2015-07-30 13:19:12      阅读:175      评论:0      收藏:0      [点我收藏+]

 

public static void main(String[] args) {
Gson gson = new Gson();
List<Person> persons = new ArrayList<Person>();
for (int i = 0; i < 10; i++) {
Person p = new Person();
p.setName("name" + i);
p.setAge(i * 5);
persons.add(p);
}
String str = gson.toJson(persons);
System.out.println(str);
Type type = new TypeToken<List>() {
}.getType();
List list = gson.fromJson(str, type);
for (Iterator it = list.iterator(); it.hasNext();) {
System.out.println(it.next());
}
}

static class Person {

private String name;
private int age;

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the age
*/
public int getAge() {
return age;
}

/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return name + ":" + age;
}
}

Gson简要使用

原文:http://www.cnblogs.com/Junze/p/4688680.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!