Gson学习笔记

本文介绍了Gson库在Java中处理Json数据的优势,相较于C中的cJSON库,Gson提供了更方便的对象转换功能。通过示例展示了如何使用Gson的fromJson方法将Json字符串解析为Java对象,以及如何使用toJson方法将对象转换为Json字符串。此外,还提到了JsonParser的使用以及注意事项。
摘要由CSDN通过智能技术生成

Gson是一个用于处理Json相关数据的Java库,类似于C中的cJSON库。(cJSON学习笔记

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.

相比于cJSON,Gson更加的抽象,开发者可以直接以类为单位进行Json数据的解析/生成,而不需要关心每个Json对象的数据类型。

安装

在Gradle中加入如下配置:

dependencies {
  implementation 'com.google.code.gson:gson:2.9.0'
}

fromJson

fromJson能够将json数据流转换为指定的java(泛型)类型对象。例如以下json数据:

{
	"name":"XiaoMing",
	"age":21,
	"id":"10001",
	"isOnline":true,
	"length":18.5
}

如果使用cJSON库来解析以上json数据流,则需要根据每个json对象的key与类型来进行相应的值的获取:

cJSON *json_name = cJSON_GetObjectItem(json, "name");
cJSON *json_age = cJSON_GetObjectItem(json, "age");
......
    
char* name = json_name->valuestring;
int age = json_age->valueint;
......

而使用Gson库来解析以上json数据流,则只需要根据报文内容定义好相关的类,再使用fromJson函数,即可将json数据流解析到指定的类型对象中:

public class UserInfo {
    String name;
    int age;
    boolean isOnline;
    float length;
    String testItem;
}

public void ParseJsonData(){
    String jsonData = "{\"name\":\"XiaoMing\",\"age\":21,\"id\":\"10001\",\"isOnline\":true,\"length\":18.5}";
    Gson gson = new Gson();
    UserInfo userInfo = gson.fromJson(jsonData, UserInfo.class);
    System.out.println("name: " + userInfo.name);
    System.out.println("age: " + userInfo.age);
    System.out.println("isOnline: " + userInfo.isOnline);
    System.out.println("length: " + userInfo.length);
    System.out.println("testItem: " + userInfo.testItem);
}

以上代码的打印输出为:

I/System.out: name: XiaoMing
I/System.out: age: 21
I/System.out: isOnline: true
I/System.out: length: 18.5
I/System.out: testItem: null

需要注意的是:

  • Gson的fromJson方法只会根据类中的成员名称,“尽量地”解析Json数据流,例如上述例子中的UserInfo类没有"id"这个成员,同时具有"testItem"这个成员,那么数据流中的"id"就不会被解析至userInfo中,而userInfo.testItem的值也为null;

JsonParser.parseString()

当开发者只需要json数据流中的某个对象时,无需特地根据原始的json数据流定义一个泛型类,可以直接使用JsonParser.parseString(String)方法进行数据解析:

public void ParseJsonData(){
    String jsonData = "{\"name\":\"XiaoMing\",\"age\":21,\"id\":\"10001\",\"isOnline\":true,\"length\":18.5}";
    JsonObject jsonObject = (JsonObject) JsonParser.parseString(jsonData);
    System.out.println("name: " + jsonObject.get("name").getAsString());
    System.out.println("age: " + jsonObject.get("age").getAsInt());
    System.out.println("isOnline: " + jsonObject.get("isOnline").getAsBoolean());
    System.out.println("length: " + jsonObject.get("length").getAsFloat());
}

以上代码的输出为:

I/System.out: name: XiaoMing
I/System.out: age: 21
I/System.out: isOnline: true
I/System.out: length: 18.5

需要注意的是:

  • JsonParser类已经被标记为Deprecated,开发者只需要使用parseString的静态方法即可解析json数据流,详见官方API文档
ConstructorDescription
JsonParser()**Deprecated.**No need to instantiate this class, use the static methods instead.

toJson

与fromJson相反,toJson的作用为将指定的(泛型)类转换为符合json规范的字符串:

public class UserInfo {
    String name;
    int age;
    boolean isOnline;
    float length;
    String testItem;
    public UserInfo(String testItem, int age, boolean isOnline, float length) {
        this.testItem = testItem;
        this.age = age;
        this.isOnline = isOnline;
        this.length = length;
    }
}
public void ParseJsonData() {
    Gson gson = new Gson();
    UserInfo userInfo = new UserInfo("TestName", 50, false, 13);
    String outStr = gson.toJson(userInfo);
    System.out.println("outStr: " + outStr);
}

以上程序的输出内容为:

I/System.out: outStr: {"age":50,"isOnline":false,"length":13.0,"testItem":"TestName"}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值