相当强大,不仅可以转基本类型,还可以把相应格式的字符串转为list或map
public static void main(String[] args) {
Gson gson = new Gson();
//1.将字符串转为int(在去掉双引号上感觉很强大)
int i = gson.fromJson("\"250\"", int.class);
System.out.println(i);
//2.将字符串转为double(在去掉双引号上感觉很强大)
double d = gson.fromJson("\"99.99\"", double.class);
System.out.println(d);
//3.将字符串转为boolean类型
boolean b = gson.fromJson("true", boolean.class);
System.out.println(b);
//4.将字符串转为字符串类型(在去掉双引号上感觉很强大,但也存在限制,
// 比如多个双引号时会报错)
System.out.println("\"String\"");
String str = gson.fromJson("\"String\"", String.class);
System.out.println(str);
System.out.println("\"I feel very \"good \"\"");
//输出 ; "I feel very "good ""
String str2 = gson.fromJson("\"I feel very \"good \"", String.class);
//报错 : com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException
// 5.将json格式的字符串转为map,但map中的value值如果还包含map格式或List型格式的值,则会被转换为
// class com.google.gson.internal.LinkedTreeMap或java.util.ArrayList,且之后下一级仍会被转换,而不
// 是简单地用字符串代替(很强大)
Map map = gson.fromJson("{\n" +
" \"date\":\"2019-02-25\",\n" +
" \"data\":[\n" +
" [\n" +
" {\n" +
" \"name\":\"why\",\n" +
" \"age\":18\n" +
" },\n" +
" {\n" +
" \"name\":\"xm\",\n" +
" \"age\":19\n" +
" }\n" +
" ],\n" +
" [\n" +
" {\n" +
" \"name\":\"xiaohong\",\n" +
" \"age\":20\n" +
" },\n" +
" {\n" +
" \"name\":\"xiaogou\",\n" +
" \"age\":21\n" +
" }\n" +
" ]\n" +
" ],\n" +
" \"school\":{\n" +
" \"address\":\"北京\",\n" +
" \"district\":\"昌平\"\n" +
" }\n" +
"}", Map.class);
System.out.println(map.get("school"));
//输出 : {address=北京, district=昌平}
System.out.println(map.get("school").getClass());
//输出 : class com.google.gson.internal.LinkedTreeMap
System.out.println(((Map) map.get("school")).get("address"));
//输出 : 北京
System.out.println(map.get("data"));
//输出 : [[{name=why, age=18.0}, {name=xm, age=19.0}], [{name=xiaohong, age=20.0}, {name=xiaogou, age=21.0}]]
System.out.println(map.get("data").getClass());
//输出 : class java.util.ArrayList
System.out.println(((List)map.get("data")).get(0));
//输出 : [{name=why, age=18.0}, {name=xm, age=19.0}]
System.out.println((((List)map.get("data")).get(0)).getClass());
//输出 : class java.util.ArrayList
System.out.println(((List)(((List)map.get("data")).get(0))).get(0).getClass());
//输出 : class com.google.gson.internal.LinkedTreeMap
//6.实体类字符串转实体类
String usrStr = "{\"hisName\":\"why\",\"hisAge\":18,\"hisWeight\":100.0}";
User usr = gson.fromJson(usrStr, User.class);
System.out.println(usr.getHisName());
//输出 : why
}
1.将Object转为字符串,如果obj为json格式,某个key值为null,则不包含
public static String objectToString(Object obj) {
Gson gson = new Gson();
String jsonObject = gson.toJson(obj);
return jsonObject;
}
2.将Object转为字符串,如果obj为json格式,某个key值为null也包含
public static String objToStringIncludeNull(Object obj) {
GsonBuilder gsonBuilder = new GsonBuilder();
String jsonString = gsonBuilder.serializeNulls().create().toJson(obj);
return jsonString;
}
public static void main(String[] args) {
Map map = new HashMap();
map.put("name","why");
map.put("age","27");
map.put("sex",null);
map.put("address","");
System.out.println(map);
//输出 : {address=, sex=null, name=why, age=27}
System.out.println(map.toString());
//输出 : {address=, sex=null, name=why, age=27}
System.out.println(objectToString(map));
//输出 : {"address":"","name":"why","age":"27"}
System.out.println(objToStringIncludeNull(map));
//输出 : {"address":"","sex":null,"name":"why","age":"27"}
User user = new User();
user.setHisAge(18);
// 字符串没设置值的话会默认为null,数字没设置值得话默认为0
System.out.println(objectToString(user));
//输出 :{"hisAge":18,"hisWeight":0.0}
System.out.println(objToStringIncludeNull(user));
//输出 :{"hisName":null,"hisAge":18,"hisWeight":0.0}
}