如何在java中使用com.google.gson.JsonObject将值设置为null?

时间:2021-11-12 23:08:57

How do you set a value to null with com.google.gson.JsonObject in java? I can certainly read if a value is null by using isNull - but it seems like when I put null, it just ignores it:

如何在java中使用com.google.gson.JsonObject将值设置为null?我当然可以通过使用isNull来读取值是否为null - 但似乎当我放入null时,它只是忽略它:

JSONObject jsonObj = new JSONObject();
jsonobjToEra.addProperty("sun", Double.parseDouble(val));
jsonobjToEra.addProperty("mon", 22.333);
jsonobjToEra.addProperty("val", null); //ignoring it

Please note that I am using com.google.gson.JsonObject which is different from similar questions like How do you set a value to null with org.json.JSONObject in java? in other posts in this SO.

请注意,我使用的是com.google.gson.JsonObject,它与类似的问题不同,例如如何在java中使用org.json.JSONObject将值设置为null?在此SO的其他帖子中。

The JSON:

[
  {
    "sun": 1122435343453,
    "mon": 1460538600000,
    "val": 45.900001525878906
  },
  {
    "sun": 1460538600000,
    "mon": 1460538660000,
    "val": 45.900001525878906
  }
  ]

3 个解决方案

#1


7  

You can use com.google.gson.JsonNull.INSTANCE like this:

您可以像这样使用com.google.gson.JsonNull.INSTANCE:

JsonObject jsonObj = ...
jsonObj.add("val", JsonNull.INSTANCE); 

Or if you want to complety remove value from serialization

或者,如果您想完成从序列化中删除值

jsonObj.remove("val");

Demo:

String json = "{\"a\": 1, \"b\": 1}";
JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class);

System.out.println(jsonObject); // {"a":1,"b":1}

jsonObject.addProperty("a", 2);
jsonObject.add("b", JsonNull.INSTANCE);
System.out.println(jsonObject); // {"a":2,"b":null}

jsonObject.remove("a");
System.out.println(jsonObject); // {"b":null}

#2


7  

You need to use serializeNulls() of GsonBuilder. It enables Gson to serialize null fields.

你需要使用GsonBuilder的serializeNulls()。它使Gson能够序列化空字段。

You can read more at here and here

您可以在这里和这里阅读更多内容

EDIT

Usage of GsonBuilder(From Official documentation)

GsonBuilder的用法(来自官方文档)

Gson gson = new GsonBuilder()
     .serializeNulls()
     .create();

Gson is made for converting Java objects to/from JSON. So you need to make a Data Structure or a class so that you can make an object of that and convert the whole object to json with null.

Gson用于将Java对象转换为JSON或从JSON转换。因此,您需要创建一个数据结构或类,以便您可以创建一个对象并将整个对象转换为带有null的json。

See this example

看这个例子

public class Country {

    String name;
    int population;
    private List<String> listOfStates;

    //getter and setter methods

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPopulation() {
        return population;
    }

    public void setPopulation(int population) {
        this.population = population;
    }

    public List<String> getListOfStates() {
        return listOfStates;
    }

    public void setListOfStates(List<String> listOfStates) {
        this.listOfStates = listOfStates;
    }

}

And when you use it:

当你使用它时:

Country countryObj=new Country();
  countryObj.setName("India");
  countryObj.setPopulation(1000000);
  List<String> listOfStates=new ArrayList<String>();
  listOfStates.add("Madhya Pradesh");
  listOfStates.add("Maharastra");
  listOfStates.add("Rajasthan");

  countryObj.setListOfStates(listOfStates);
  Gson gson = new GsonBuilder()
     .serializeNulls()
     .create();

  // convert java object to JSON format,
  // and returned as JSON formatted string
  String json = gson.toJson(countryObj);

#3


-1  

did you try setProperty("",null) ?

你尝试过setProperty(“”,null)吗?

#1


7  

You can use com.google.gson.JsonNull.INSTANCE like this:

您可以像这样使用com.google.gson.JsonNull.INSTANCE:

JsonObject jsonObj = ...
jsonObj.add("val", JsonNull.INSTANCE); 

Or if you want to complety remove value from serialization

或者,如果您想完成从序列化中删除值

jsonObj.remove("val");

Demo:

String json = "{\"a\": 1, \"b\": 1}";
JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class);

System.out.println(jsonObject); // {"a":1,"b":1}

jsonObject.addProperty("a", 2);
jsonObject.add("b", JsonNull.INSTANCE);
System.out.println(jsonObject); // {"a":2,"b":null}

jsonObject.remove("a");
System.out.println(jsonObject); // {"b":null}

#2


7  

You need to use serializeNulls() of GsonBuilder. It enables Gson to serialize null fields.

你需要使用GsonBuilder的serializeNulls()。它使Gson能够序列化空字段。

You can read more at here and here

您可以在这里和这里阅读更多内容

EDIT

Usage of GsonBuilder(From Official documentation)

GsonBuilder的用法(来自官方文档)

Gson gson = new GsonBuilder()
     .serializeNulls()
     .create();

Gson is made for converting Java objects to/from JSON. So you need to make a Data Structure or a class so that you can make an object of that and convert the whole object to json with null.

Gson用于将Java对象转换为JSON或从JSON转换。因此,您需要创建一个数据结构或类,以便您可以创建一个对象并将整个对象转换为带有null的json。

See this example

看这个例子

public class Country {

    String name;
    int population;
    private List<String> listOfStates;

    //getter and setter methods

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPopulation() {
        return population;
    }

    public void setPopulation(int population) {
        this.population = population;
    }

    public List<String> getListOfStates() {
        return listOfStates;
    }

    public void setListOfStates(List<String> listOfStates) {
        this.listOfStates = listOfStates;
    }

}

And when you use it:

当你使用它时:

Country countryObj=new Country();
  countryObj.setName("India");
  countryObj.setPopulation(1000000);
  List<String> listOfStates=new ArrayList<String>();
  listOfStates.add("Madhya Pradesh");
  listOfStates.add("Maharastra");
  listOfStates.add("Rajasthan");

  countryObj.setListOfStates(listOfStates);
  Gson gson = new GsonBuilder()
     .serializeNulls()
     .create();

  // convert java object to JSON format,
  // and returned as JSON formatted string
  String json = gson.toJson(countryObj);

#3


-1  

did you try setProperty("",null) ?

你尝试过setProperty(“”,null)吗?