java解析和生成GSON串

来源:互联网 发布:淘宝小号购买实名认证 编辑:程序博客网 时间:2024/06/06 12:49

一、需要的包

谷歌提供的:gson-2.5.1.jar,下载地址:http://download.csdn.net/detail/qq_25837957/9316521点击打开链接


二、生成gson串

1、避免回环情况

GsonBuilder gb=new GsonBuilder();

Gson  gson=gb.create();

String  result=gson.toJson("hello world");

System.out.print("gson串:"+result);

2、可能导致回环(不建议使用)

Gson  gson=new Gson();

String  result=gson.toJson("hello world");

System.out.print("gson串:"+result);


三、解析gson串

实质上是使用了一个方法fromJson();

String str="{'hhhaksd'}";
Gson gson=new Gson();
String  res=gson.fromJson(str, String.class);
注意:如果gson是对象或者其它数据结构,那么需要改变!



0 0