博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Gson常用方法
阅读量:5107 次
发布时间:2019-06-13

本文共 4652 字,大约阅读时间需要 15 分钟。

在开发中我们经常通过Gson来解析字符串,转化为所需要的Java对象。使用到它的地方很多,常用的几个方法总结如下:

1.字符串转实体类

public 
T fromJson(JsonElement json, Class
classOfT) throws JsonSyntaxException

This method deserializes the Json read from the specified parse tree into an object of the specified type.

示例:

/**     * str ---> bean     */    private void str2bean(){        String str = "{'address':'xxx街xxx号','deptList':[{'employeeNum':'0','name':'部门0','task':'研发0'},{'employeeNum':'10','name':'部门1','task':'研发1'},{'employeeNum':'20','name':'部门2','task':'研发2'}],'name':'xxx公司'}";        Company cpy = new Gson().fromJson(str, Company.class);        Log.e("bean", cpy.toString());    }

2.实体类转字符串

String  toJson(Object src)

This method serializes the specified object into its equivalent Json representation.

示例:

/**     * bean ---> Json字符串     */    private void bean2str(){        Company cpy = new Company();        List
list = new ArrayList
(); for(int i=0; i<3;i++){
Department dpt = new Department(); dpt.setName("部门"+i); dpt.setEmployeeNum("" + i * 10); dpt.setTask("研发"+i); list.add(dpt); } cpy.setName("xxx公司"); cpy.setAddress("xxx街xxx号"); cpy.setDeptList(list); String str = new Gson().toJson(cpy); Log.e("Company", str); } // {
"address":"xxx街xxx号","deptList":[{
"employeeNum":"0","name":"部门0","task":"研发0"},{
"employeeNum":"10","name":"部门1","task":"研发1"},{
"employeeNum":"20","name":"部门2","task":"研发2"}],"name":"xxx公司"}

3.字符串转JsonObject

JsonElement parse(String json)

Parses the specified JSON string into a parse tree

示例:

/**     * str ---> JsonObject     */    private void str2JsonObject() {        String str = "{'address':'xxx街xxx号','deptList':[{'employeeNum':'0','name':'部门0','task':'研发0'},{'employeeNum':'10','name':'部门1','task':'研发1'},{'employeeNum':'20','name':'部门2','task':'研发2'}],'name':'xxx公司'}";        JsonObject returnData = new JsonParser().parse(str).getAsJsonObject();        Log.e("JsonObject", returnData.toString());        //{
"address":"xxx街xxx号","deptList":[{
"employeeNum":"0","name":"部门0","task":"研发0"},{
"employeeNum":"10","name":"部门1","task":"研发1"},{
"employeeNum":"20","name":"部门2","task":"研发2"}],"name":"xxx公司"} }

4.字符串转JsonArray

/**     * str ---> JsonArray     */    private void str2JsonArray() {        String arrStr = "[{'employeeNum':'0','name':'部门0','task':'研发0'},{'employeeNum':'10','name':'部门1','task':'研发1'},{'employeeNum':'20','name':'部门2','task':'研发2'}]";        JsonArray jsArray = new JsonParser().parse(arrStr).getAsJsonArray();        Log.e("JsonArray", jsArray.get(0).getAsJsonObject().get("name").getAsString());    }

5.字符串转JsonArray实体

/**     * str ---> JsonArrayBean     */    private void str2JsonArrayBean(){        String arrStr = "[{'employeeNum':'0','name':'部门0','task':'研发0'},{'employeeNum':'10','name':'部门1','task':'研发1'},{'employeeNum':'20','name':'部门2','task':'研发2'}]";        Gson gson = new Gson();        JsonParser parser = new JsonParser();        JsonArray jsArray = parser.parse(arrStr).getAsJsonArray();        ArrayList
dptList = new ArrayList
(); for(JsonElement obj : jsArray){ Department dpt = gson.fromJson( obj , Department.class); dptList.add(dpt); Log.e("Dpt", dpt.toString()); } }

涉及到的测试Java实体如下:

package com.example.gsontest;import java.util.List;public class Company {
private String name; private String address; private List
deptList; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public List
getDeptList() { return deptList; } public void setDeptList(List
deptList) { this.deptList = deptList; }}
package com.example.gsontest;public class Department {    private String name;    private String employeeNum;    private String task;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getEmployeeNum() {        return employeeNum;    }    public void setEmployeeNum(String employeeNum) {        this.employeeNum = employeeNum;    }    public String getTask() {        return task;    }    public void setTask(String task) {        this.task = task;    }}

转载于:https://www.cnblogs.com/lishbo/p/9956007.html

你可能感兴趣的文章
Thrift Expected protocol id ffffff82 but got 0
查看>>
【2.2】创建博客文章模型
查看>>
从零开始系列之vue全家桶(1)安装前期准备nodejs+cnpm+webpack+vue-cli+vue-router
查看>>
Jsp抓取页面内容
查看>>
大三上学期软件工程作业之点餐系统(网页版)的一些心得
查看>>
可选参数的函数还可以这样设计!
查看>>
[你必须知道的.NET]第二十一回:认识全面的null
查看>>
Java语言概述
查看>>
关于BOM知识的整理
查看>>
使用word发布博客
查看>>
面向对象的小demo
查看>>
微服务之初了解(一)
查看>>
GDOI DAY1游记
查看>>
收集WebDriver的执行命令和参数信息
查看>>
数据结构与算法(三)-线性表之静态链表
查看>>
mac下的mysql报错:ERROR 1045(28000)和ERROR 2002 (HY000)的解决办法
查看>>
MyBaits动态sql语句
查看>>
HDU4405(期望DP)
查看>>
拉格朗日乘子法 那些年学过的高数
查看>>
vs code 的便捷使用
查看>>