简体   繁体   English

使用com.google.gson进行数据输入

[英]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. LinkedTreeMapJsonObject结构通过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.

相关问题 如何使用com.google.gson类创建json对象 - How to create json object using com.google.gson class 为什么JsonParser使用com.google.gson API在返回值中使用双引号引起来 - Why JsonParser gives double quotes in the return value, using com.google.gson API 如何在JsonArray(com.google.gson)中将Empty字符串转换为null - How to convert Empty string to null in JsonArray ( com.google.gson) 如何修复 Android Studio 中的“无法解析:com.google.gson:gson:2.8.5” - How to fix "Failed to resolve: com.google.gson:gson:2.8.5" in Android studio 无法使用 gson.toJson(Element) 获取 JSON 字符串,{模块 java.base 不会“打开 java.util”到模块 com.google.gson] - Can't get JSON String by using gson.toJson(Element), {module java.base does not "opens java.util" to module com.google.gson] java maven 导入问题:错误:com.google.gson 包不存在 - java maven import issue: error: package com.google.gson does not exist 编译错误-在Redhat“ Openshift”应用程序中找不到com.google.gson包 - Compilation error - package com.google.gson not found in Redhat “Openshift” application 错误:包com.google.gson不存在(在playframwork 2.0中) - error: package com.google.gson does not exist (in play framwork 2.0) package com.google.gson 在 linux 上的 Visual Studio 代码中不存在 - package com.google.gson does not exist in visual studio code on linux GSON“NoClassDefFoundError com/google/gson/Gson” - GSON “NoClassDefFoundError com/google/gson/Gson”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM