[英]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.