java 在 Gson 中,如何将 JsonArray 添加到 JsonObject?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38642868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Gson, how do I add a JsonArray to a JsonObject?
提问by Cache Staheli
Suppose that I am trying to get a Json structure like this:
假设我正在尝试获得这样的 Json 结构:
{
"rows": [
{
"date_str": "2016-07-01",
"sleep_7_or_more_hours": true,
"activity_minutes": "0",
"water_5_or_more_cups": true,
"fruit_veg_4_or_more_servings": true,
"physical_activity_description": "walking"
}
{
"date_str": "2016-07-02",
"sleep_7_or_more_hours": true,
"activity_minutes": "30",
"water_5_or_more_cups": true,
"fruit_veg_4_or_more_servings": true,
"physical_activity_description": "walking"
}
...etc
]
}
Some questions about building this Json:
关于构建这个 Json 的一些问题:
- How can I specify the name of the
JsonArray
? I need it to be named, in the Json,"rows"
. - How can I add the
JsonArray
rows
to aJsonObject
(I assume that's what the outer brackets mean)?
- 如何指定 的名称
JsonArray
?我需要在 Json 中命名它,"rows"
. - 如何将 the 添加
JsonArray
rows
到 aJsonObject
(我认为这就是外括号的意思)?
This is the code that I am using to do it:
这是我用来执行此操作的代码:
JsonArray rows = new JsonArray();
//Code to get local dates omitted
for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1))
{
JsonObject row = new JsonObject();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-mm-dd", Locale.ENGLISH);
String dateString = date.format(formatter);
row.addProperty("date_str", dateString);
boolean sleptLongEnough = (sleepLog.getTimeInBed(getDate(date)) > (7 * 60));
row.addProperty("sleep_7_or_more_hours", sleptLongEnough);
int activityMinutes = (activitiesLog.getMinutesVeryActive(getDate(date)) + activitiesLog.getMinutesFairlyActive(getDate(date)));
...
//Omitted extra code
rows.add(row);
}
JsonObject logs = new JsonObject();
//add rows to logs here.
I need to add rows
to logs
. However, JsonObject
only appears to have .add(JsonElement)
and .addProperty(String, variousTypes)
, and nothing to add an array. What am I missing?
我需要添加rows
到logs
. 但是,JsonObject
似乎只有.add(JsonElement)
和.addProperty(String, variousTypes)
,并没有添加数组。我错过了什么?
EDIT:I am not using Gson to serialize objects because the Json is composed of data items from each of several logs (and not even close to all of the information in each log).
编辑:我没有使用 Gson 来序列化对象,因为 Json 由来自多个日志中的每一个的数据项组成(甚至不接近每个日志中的所有信息)。
回答by thewmo
JsonArray
is an instance of JsonElement
. So the method .add("name", element)
, where element is a JsonArray
, should just work.
JsonArray
是 的一个实例JsonElement
。因此.add("name", element)
,元素为 a的方法JsonArray
应该可以正常工作。
回答by Jeeter
You can use the method JsonObject.add()
to add a JsonElement
, which JsonArray
inherits from:
您可以使用该方法JsonObject.add()
添加一个JsonElement
,它JsonArray
继承自:
logs.add("rows", rows)
And then when deserializing it out, you can just cast it back to a JsonArray
然后当反序列化它时,你可以将它转换回 JsonArray