现在的位置: 首页 > 操作系统 > 正文

Java解析和生成GSON串

2020年02月12日 操作系统 ⁄ 共 914字 ⁄ 字号 评论关闭

Java解析和生成GSON串

一、需要的包

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

到Linux公社资源站下载:

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

免费下载地址在 http://linux.xuebuyuan.com/

用户名与密码都是www.xuebuyuan.com

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

下载方法见 http://www.xuebuyuan.com/Linux/2013-07/87684.htm

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

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

本文永久更新链接地址:http://www.xuebuyuan.com/Linux/2016-12/138516.htm

以上就上有关Java解析和生成GSON串的全部内容,学步园全面介绍编程技术、操作系统、数据库、web前端技术等内容。

抱歉!评论已关闭.