Java解析和生成GSON串

Java解析和生成GSON串

一、需要的包

使用Google Gson解析Json数据时需要使用的 gson-2.5.jar,下载地址:

到Linux公社资源站下载:

------------------------------------------分割线------------------------------------------

具体下载目录在 /2016年资料/12月/19日/Java解析和生成GSON串/

------------------------------------------分割线------------------------------------------

二、生成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是对象或者其它数据结构,那么需要改变!

猜你喜欢

转载自www.linuxidc.com/Linux/2016-12/138516.htm