Reputation: 1224
I have a JsonObject that contains a JsonArray of arrays. I would like to parse it using Gson. The following is what the data looks like. I can use JsonArray data = resourceObj.get("data").getAsJsonArray()
to get the outer array, but I m not sure how to access the inner array. My understanding is that to get something as a JsonArray it has to be in the form "xyz": [ something] or "xyz"=[something]
{
"sid": "123456",
"data": [
[
1595808000,
0
],
[
1595894400,
0
],
[
1595980800,
0.0829
],
[
1596067200,
0.0047
]
],
}
Upvotes: 1
Views: 210
Reputation: 12235
Also: if you have a string to parse so not using actual JsonObject and having such a stable JSON you can declare a wrapping data class to make handling more convenient and perhaps more type safe.
So something like this:
@Getter
@Setter
public static class MyData {
private Long sid;
@SuppressWarnings("serial")
public static class DataItem extends ArrayList<Object> {
public LocalDateTime getSomeLocalDateTime() {
return Instant
.ofEpochSecond(((Double)get(0)).longValue())
.atZone(ZoneId.systemDefault())
.toLocalDateTime();
}
public Double getSomeDouble() {
return (Double)get(1);
}
}
private List<DataItem> data;
}
Use:
MyData myData = getGson().fromJson(json, MyData.class);
LocalDateTime ldt = myData.getData().get(0).getDate();
Double dbl = myData.getData().get(0).getSomeDouble();
Upvotes: 1
Reputation: 159165
The value of data
is an array of arrays.
E.g. the value of data[2][1]
is 0.0829
.
What the second value of the inner array is, I don't know, but the first value looks like it is a Unix Timestamp.
E.g. value 1595980800
is Wednesday, July 29, 2020 12:00:00 AM GMT
.
Upvotes: 1