[英]Data entry using com.google.gson
I'm trying to create a Gson object which will contain differents categories and entries Here is the sample i'm trying to do: 我正在尝试创建一个Gson对象,其中将包含不同的类别和条目,这是我尝试执行的示例:
JsonObject jo = new JsonObject();
JsonArray ja = new JsonArray();
JsonObject mainObj = new JsonObject();
jo.addProperty("firstName", "John");
jo.addProperty("lastName", "Doe");
ja.add(jo);
mainObj.add("employees", ja);
jo = new JsonObject();
ja = new JsonArray();
jo.addProperty("firstName", "jean");
jo.addProperty("lastName", "dorian");
ja.add(jo);
mainObj.add("employees", ja);
jo = new JsonObject();
ja = new JsonArray();
jo.addProperty("firstName", "toto");
jo.addProperty("lastName", "tata");
ja.add(jo);
mainObj.add("manager", ja);
The problem is has you can see I have to create every time a new JSonObject and Array which is I believe not the best practice and also the old value in "employees" is replacing by the second. 问题是您可以看到我每次必须创建一个新的JSonObject和Array时,我认为这不是最佳实践,而且“员工”中的旧值也被第二个替换。 Someone can help me on this please? 有人可以帮我吗?
Br, 溴
Jérémie 杰里米
I have to create every time a new JSonObject and Array which is I believe not the best practice 我每次都必须创建一个新的JSonObject和Array,我认为这不是最佳实践
I think it's perfectly fine to do it like this. 我认为这样做是完全可以的。
and also the old value in "employees" is replacing by the second 而且“员工”中的旧值也被第二个值所取代
The problem is that you want to add a mapping "employees" -> JsonArray
two times in your JsonObject
. 问题是您想在JsonObject
两次添加一个映射"employees" -> JsonArray
。 While online JSON parsers such as JSONLint don't say anything about that, it's actually not recommended to have two identical keys in a JsonObject
. 尽管在线JSON解析器(例如JSONLint)对此没有说明,但实际上不建议在JsonObject
两个相同的键。
This is explained in the RFC 7159 , chapter 4: RFC 7159第4章对此进行了解释:
An object whose names are all unique is interoperable in the sense that all software implementations receiving that object will agree on the name-value mappings. 从名称接收到的所有软件实现都将在名称-值映射上达成一致的意义上来说,名称都是唯一的对象是可以互操作的。 When the names within an object are not unique, the behavior of software that receives such an object is unpredictable. 如果对象中的名称不是唯一的,则接收到该对象的软件的行为是不可预测的。 Many implementations report the last name/value pair only. 许多实现仅报告姓/值对。 Other implementations report an error or fail to parse the object, and some implementations report all of the name/value pairs, including duplicates. 其他实现报告错误或无法解析对象,某些实现报告所有名称/值对,包括重复项。
Under the hood, the JsonObject
structure is implemented with a LinkedTreeMap
to save the mappings. 在LinkedTreeMap
, JsonObject
结构通过LinkedTreeMap
实现以保存映射。 When you add a new mapping, the put
method is called, which will erase the previous mapped value, if any. 添加新映射时, put
调用put
方法,该方法将删除以前的映射值(如果有)。
90 @Override public V put(K key, V value) {
91 if (key == null) {
92 throw new NullPointerException("key == null");
93 }
94 Node<K, V> created = find(key, true);
95 V result = created.value;
96 created.value = value;
97 return result;
98 }
If you want to add another Employee
to the array, you shouldn't add it directly to the JsonObject
and create a new JsonArray
. 如果要向该数组添加另一个Employee
,则不应将其直接添加到JsonObject
并创建一个新的JsonArray
。
JsonObject jo = new JsonObject();
JsonArray ja = new JsonArray();
JsonObject mainObj = new JsonObject();
jo.addProperty("firstName", "John");
jo.addProperty("lastName", "Doe");
ja.add(jo);
//remove this line
mainObj.add("employees", ja);
jo = new JsonObject();
//and remove this line
ja = new JsonArray();
jo.addProperty("firstName", "jean");
jo.addProperty("lastName", "dorian");
ja.add(jo);
mainObj.add("employees", ja);
jo = new JsonObject();
ja = new JsonArray();
jo.addProperty("firstName", "toto");
jo.addProperty("lastName", "tata");
ja.add(jo);
mainObj.add("manager", ja);
which will result in: 这将导致:
{
"employees": [
{
"firstName": "John",
"lastName": "Doe"
},
{
"firstName": "jean",
"lastName": "dorian"
}
],
"manager": [
{
"firstName": "toto",
"lastName": "tata"
}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.