{"code":0,"msg":"","data":{"Data":{"ExpireAt":1686713189,"Token":"3d1b82f87924442982dac6617c4d7547"},"RequestId":"9db12749-94cb-49c0-94bf-926805a65f7d"}} {"code":0,"msg":"","data":{"DeviceList":[{"ProductId":"G0K1O9H4OX","DeviceName":"58d200111d029ff09c218a8fb7ed2111","DeviceId":"G0K1O9H4OX/58d200111d029ff09c218a8fb7ed2111","AliasName":"Qp3DPhotoFrame","UserID":"504315054565363712","RoomId":"0","FamilyId":"f_630c05b436104a88af0508cc881f8130","IconUrl":"https://main.qcloudimg.com/raw/05ca75c84bb7c1e2dbc9d762cf3af1f1.png","IconUrlGrid":"https://main.qcloudimg.com/raw/05ca75c84bb7c1e2dbc9d762cf3af1f1.png","DeviceType":0,"CreateTime":1686041240,"UpdateTime":1686041240,"Online":1}],"RequestId":"2da63815-2e66-40f3-b4fd-9f304cd1502b","Total":1}} Gson解析通用怎么写
时间: 2023-08-10 18:04:53 浏览: 403
您的问题是如何使用 Gson 解析这两个 JSON 字符串。首先需要确保您已经引入了 Gson 库。
对于第一个 JSON 字符串,可以定义如下的实体类:
```
public class TokenData {
private int code;
private String msg;
private Data data;
public static class Data {
private ExpireAt;
private String Token;
// getter and setter
}
// getter and setter
}
```
然后使用 Gson 将 JSON 字符串转换为实体类对象:
```
Gson gson = new Gson();
TokenData tokenData = gson.fromJson(jsonString, TokenData.class);
```
对于第二个 JSON 字符串,可以定义如下的实体类:
```
public class DeviceListData {
private int code;
private String msg;
private Data data;
public static class Data {
private List<Device> DeviceList;
private String RequestId;
private int Total;
// getter and setter
}
public static class Device {
private String ProductId;
private String DeviceName;
private String DeviceId;
private String AliasName;
private String UserID;
private String RoomId;
private String FamilyId;
private String IconUrl;
private String IconUrlGrid;
private int DeviceType;
private long CreateTime;
private long UpdateTime;
private int Online;
// getter and setter
}
// getter and setter
}
```
然后使用 Gson 将 JSON 字符串转换为实体类对象:
```
Gson gson = new Gson();
DeviceListData deviceListData = gson.fromJson(jsonString, DeviceListData.class);
```
阅读全文