Gson,大家在都知道是json解析工具,也是google的亲儿子,自然也有着诸多的优点,今天就来总结一下其详细的用法,在本篇中将讲解其基础的使用方法,下一篇将讲解其高级的用法。
1、基本数据类型解析,例:
Gson gson = new Gson(); int i = gson.fromJson( "1", int.class); //1 double d = gson.fromJson( "\"1.11\"", double.class); //1.11 double d2 = gson.fromJson( "1.11", double.class); //1.11 boolean b = gson.fromJson( "true", boolean.class); // true String s = gson.fromJson( "str", String.class); // <span style="font-family: Arial, Helvetica, sans-serif;">str</span>
2、基本数据类型生成,例:
Gson gson = new Gson(); String s1= gson.toJson( 1); // 1 String s2= gson.toJson( false); // false String s3= gson.toJson( "str"); //"str"
3、实体类的解析及生成,例:
public class User { private String name; private boolean sex; private int age; private String homeAddress; }
User user= new User( "小明", true, 16, "中国xxxxxxxxxx"); String userJson = gson.toJson(user); Log.i(TAG,userJson); //{"name":"小明","homeAddress":"中国xxxxxxxxxx","age":16,"sex":true} User user1 = gson.fromJson(userJson, User.class); Log.i(TAG,user1.toString()); //User{homeAddress='中国xxxxxxxxxx', age=16, sex=true, name='小明'}
4、泛型在Gson中的使用:
a、数组
String jsonss = "[\"aa\",\"bb\",\"cc\"]";
String[] ss = gson.fromJson(jsonss, String[].class); for (String s:ss) { Log.i(TAG,s); } String s = gson.toJson(ss); Log.i(TAG,s);
b、List
List<String> list=gson.fromJson(jsonss, new TypeToken<List<String>>() {}.getType());
c、泛型实际开发中的应用
<pre name="code" class="java">List<User> list=gson.fromJson(jsonss, new TypeToken<List<<span style="font-family:Arial, Helvetica, sans-serif;">User</span>>>() {}.getType());
在开发过程中我们经常遇到服务器返回:
{"code":"0","message":"success","data":{}}
或者{"code":"0","message":"success","data":[]}
我们可以定义这样一个父类
public class BaseBean<T> { private String code; private Message message; private T data; }
BaseBean<User> userBase= new BaseBean<>( "0x00", "success",user); String userStr = gson.toJson(userBase); Log.i(TAG,userStr); BaseBean<User> userBase2 = gson.fromJson(userStr, new TypeToken<BaseBean<User>>() {}.getType()); Log.i(TAG,userBase2.toString()); List<User> userList= new ArrayList<>(); userList.add( new User( "11", true, 23, "中国*******")); userList.add( new User( "22", true, 23, "中国*******")); userList.add( new User( "33", true, 23, "中国*******")); userList.add( new User( "44", true, 23, "中国*******")); userList.add( new User( "55", true, 23, "中国*******")); BaseBean<List<User>> userBeanList= new BaseBean<>( "0x00", "success",userList); String userBeanListStr = gson.toJson(userBeanList); Log.i(TAG,userBeanListStr); BaseBean<List<User>> o = gson.fromJson(userBeanListStr, new TypeToken<BaseBean<List<User>>>() {}.getType()); Log.i(TAG,o.toString());
至此就已经掌握Gson的基本用法,接一下来将会总结Gson的一些进阶的用法。
<script>
(function(){
function setArticleH(btnReadmore,posi){
var winH = $(window).height();
var articleBox = $("div.article_content");
var artH = articleBox.height();
if(artH > winH*posi){
articleBox.css({
'height':winH*posi+'px',
'overflow':'hidden'
})
btnReadmore.click(function(){
articleBox.removeAttr("style");
$(this).parent().remove();
})
}else{
btnReadmore.parent().remove();
}
}
var btnReadmore = $("#btn-readmore");
if(btnReadmore.length>0){
if(currentUserName){
setArticleH(btnReadmore,3);
}else{
setArticleH(btnReadmore,1.2);
}
}
})()
</script>
</article>