简体   繁体   English

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

[英]How do you set a value to null with com.google.gson.JsonObject in java?

How do you set a value to null with com.google.gson.JsonObject in java?你如何在java中使用com.google.gson.JsonObject将值设置为null? I can certainly read if a value is null by using isNull - but it seems like when I put null, it just ignores it:我当然可以通过使用 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?请注意,我使用的com.google.gson.JsonObject与类似的问题不同,例如How do you set a value to null with org.json.JSONObject in java? in other posts in this SO.在此 SO 的其他帖子中。

The JSON: JSON:

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

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}

You need to use serializeNulls() of GsonBuilder .您需要使用GsonBuilder serializeNulls() It enables Gson to serialize null fields.它使 Gson 能够序列化空字段。

EDIT编辑

Usage of GsonBuilder GsonBuilder 的使用

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

Gson is made for converting Java objects to/from JSON. Gson 用于将 Java 对象转换为 JSON 或从 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 .所以你需要创建一个Data Structure或一个class以便你可以创建一个对象并将整个对象转换为带有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);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 com.google.gson.JsonObject无法强制转换为Java中的com.google.gson.JsonArray - com.google.gson.JsonObject cannot be cast to com.google.gson.JsonArray in Java Lotus Notes Java代理的GSON库错误-java.lang.NoClassDefFoundError:com.google.gson.JsonObject - GSON library error with Lotus Notes Java Agent - java.lang.NoClassDefFoundError: com.google.gson.JsonObject 无法加载“com.google.gson.JsonObject”类 - Unable to load class 'com.google.gson.JsonObject' com.google.gson.JsonPrimitive无法强制转换为com.google.gson.JsonObject - com.google.gson.JsonPrimitive cannot be cast to com.google.gson.JsonObject 将com.google.gson.JsonObject转换为org.json.simple.JSONObject - Convert com.google.gson.JsonObject to org.json.simple.JSONObject 用于JSON解析的Android模型显示ClassCastException:com.google.gson.JsonObject无法转换为com.google.gson.JsonArray - Android Model for JSON parsing shows ClassCastException: com.google.gson.JsonObject can not be casted to com.google.gson.JsonArray 你如何在 java 中使用 org.json.JSONObject 将值设置为 null? - How do you set a value to null with org.json.JSONObject in java? 如何在Java中使用JSONObject设置整数值? - How do you set an integer value with JSONObject in Java? 无法调用“com.google.gson.JsonElement.isJsonNull()”,因为“com.google.gson.JsonObject.get(String)”的返回值为空 - Cannot invoke "com.google.gson.JsonElement.isJsonNull()" because the return value of "com.google.gson.JsonObject.get(String)" is null 如何比较Java中JsonObject的空值 - How to compare null value from the JsonObject in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM