gson使用示例
发布时间:2020-05-24 18:24:07 所属栏目:Java 来源:互联网
导读:gson使用示例
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class People {
private int id;
private String name;
private float height;
private double weight;
private List friends = new ArrayList();
private Map info = new HashMap();
private Date birthday;
private String comment;
public People(){
id = 123456;
name = "Jacky";
height = new Float(172.2);
weight = new Double(61.236);
friends.add("Sam");
friends.add("Jimmy");
info.put("country","Taiwan");
info.put("city","Taipei");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
birthday = sdf.parse("1989-08-09");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
comment = null;
}
public String toString(){
String str = "";
str += "id="+id;
str += " name="+name;
str += " height="+height;
str += " weight="+weight;
str += " ...etc";
return str;
}
public static void main(String[] args) {
People p = new People();
//Gson gson = new Gson();
//If you wanna allow null object
Gson gson = new GsonBuilder().serializeNulls().create();
String str = gson.toJson(p);
System.out.println(str);
People p2 = gson.fromJson(str,People.class);
System.out.println(p2.toString());
}
}
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
