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文档:
Constructor Description 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"}