简体   繁体   中英

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:

        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. 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

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 . 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 .

This is explained in the RFC 7159 , chapter 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. When you add a new mapping, the put method is called, which will erase the previous mapped value, if any.

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 .

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"
    }
  ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM