如何使用GSON解析动态JSON字段?
因此,我使用 GSON 从 API 解析 JSON,但不知道如何让它解析数据中的动态字段。
下面是查询返回的 JSON 数据的示例:
{
-
30655845: {
id: "30655845"
name: "testdata
description: ""
latitude: "38"
longitude: "-122"
altitude: "0"
thumbnailURL: http://someimage.com/url.jpg
distance: 9566.6344386665
}
-
28688744: {
id: "28688744"
name: "testdata2"
description: ""
latitude: "38"
longitude: "-122"
altitude: "0"
thumbnailURL: http://someimage.com/url.jpg
distance: 9563.8328713012
}
}
我当前处理单个静态值的方式是使用类:
import com.google.gson.annotations.SerializedName;
public class Result
{
@SerializedName("id")
public int id;
@SerializedName("name")
public String name;
@SerializedName("description")
public String description;
@SerializedName("latitude")
public Double latitude;
@SerializedName("longitude")
public Double longitude;
@SerializedName("altitude")
public Double altitude;
@SerializedName("thumbnailURL")
public String thumbnailURL;
@SerializedName("distance")
public Double distance;
}
然后我可以简单地使用 GSON 来解析它:
Gson gson = new Gson();
Reader reader = new InputStreamReader(source);
Result response= gson.fromJson(reader, Result.class);
我知道这适用于子数据,因为我可以查询并获取单个条目并很容易地解析它,但是为数组中的每个值给出的随机整数值又如何呢? (即 30655845 和 2868874)
有什么帮助吗?
So I'm using GSON to parse JSON from an API and am stuck as to how to have it parse the dynamic fields in the data.
Here is an example of the JSON data returned on a query:
{
-
30655845: {
id: "30655845"
name: "testdata
description: ""
latitude: "38"
longitude: "-122"
altitude: "0"
thumbnailURL: http://someimage.com/url.jpg
distance: 9566.6344386665
}
-
28688744: {
id: "28688744"
name: "testdata2"
description: ""
latitude: "38"
longitude: "-122"
altitude: "0"
thumbnailURL: http://someimage.com/url.jpg
distance: 9563.8328713012
}
}
The way I am currently handling the single static values is with a class:
import com.google.gson.annotations.SerializedName;
public class Result
{
@SerializedName("id")
public int id;
@SerializedName("name")
public String name;
@SerializedName("description")
public String description;
@SerializedName("latitude")
public Double latitude;
@SerializedName("longitude")
public Double longitude;
@SerializedName("altitude")
public Double altitude;
@SerializedName("thumbnailURL")
public String thumbnailURL;
@SerializedName("distance")
public Double distance;
}
And then I can simply use GSON to parse that:
Gson gson = new Gson();
Reader reader = new InputStreamReader(source);
Result response= gson.fromJson(reader, Result.class);
I know this works on the sub-data as I can query and get a single entry and parse that quite easily, but what about the random integer values given for each value in the array? (ie the 30655845 and 2868874)
Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 GSON 文档,您可以执行以下操作例如:
或者您可以尝试为您的自定义序列化器编写 班级。
免责声明:我也没有使用 GSon 的经验,但没有使用 Jackson 等其他框架的经验。
According to GSON documentation you can do things like:
Or you can try to write custom serializer for your class.
Disclaimer: I too, have no experience with GSon but with other frameworks like Jackson.