I have the below JSON , i need to remove all the keys which have null value
我有以下JSON,我需要删除所有具有空值的键
I have tried this
我试过这个
import java.util.Iterator;
import org.json.JSONException;
import org.json.JSONObject;
public class Remove {
public static void main(String[] args) throws JSONException {
String str = "{\r\n" +
" \"videos\": {\r\n" +
"
" }}";
JSONObject json_obj = new JSONObject(str);
JSONObject allKeys_json= json_obj.getJSONObject("videos");
Iterator<String> keys = allKeys_json.keys();
while( keys.hasNext() ) {
String keyanme = (String)keys.next();
String keyvalue = allKeys_json.getString(keyanme);
if(keyvalue.contains("null"))
{
System.out.println(keyanme+"\t"+keyvalue);
json_obj.remove(keyanme);
}
}
System.out.println(allKeys_json);
}
}
but the actual json is unaffected , could you please tell me how to do this .
但实际的json没有受到影响,请你告诉我怎么做。
4 个解决方案
#1
1
If it's only about manipulating a string which structure you know well a solution would be to use some regex
如果它只是关于操纵字符串的结构,你知道一个解决方案就是使用一些正则表达式
str.replaceAll(".*\": null(,)?\\r\\n", "");
It could be easier to find a good regex than to invest time in building a model that could be used by Jackson.
找到一个好的正则表达式比花时间建立一个可以被杰克逊使用的模型更容易。
Three notes:
-
the code above doesn't figure out which is the last line and adapt the json accordingly.
上面的代码没有弄清楚哪一个是最后一行并相应地调整json。
-
the pattern should be compiled separately.
模式应该单独编译。
-
org.json is very inefficient compared to Jackson.
与杰克逊相比,org.json的效率非常低。
#2
1
Check your null value like this
像这样检查你的空值
if(keyvalue == null)
This fixex the issue
这解决了这个问题
#3
0
Firstly, create an model class which is corresponding to the JSON string.
首先,创建一个与JSON字符串对应的模型类。
Add
@JsonIgnoreProperties(ignoreUnknown = true)
to your model class such as
到您的模型类,如
@JsonIgnoreProperties(ignoreUnknown = true)
public class Video {
//the properties
}
http://www.baeldung.com/jackson-deserialize-json-unknown-properties
#4
0
Use Jackson API and use @JsonInclude(Include.NON_NULL) on your model/DTO class to remove.
使用Jackson API并在模型/ DTO类上使用@JsonInclude(Include.NON_NULL)进行删除。
Like
@JsonInclude(Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
public class Video {
//the properties
}
#1
1
If it's only about manipulating a string which structure you know well a solution would be to use some regex
如果它只是关于操纵字符串的结构,你知道一个解决方案就是使用一些正则表达式
str.replaceAll(".*\": null(,)?\\r\\n", "");
It could be easier to find a good regex than to invest time in building a model that could be used by Jackson.
找到一个好的正则表达式比花时间建立一个可以被杰克逊使用的模型更容易。
Three notes:
-
the code above doesn't figure out which is the last line and adapt the json accordingly.
上面的代码没有弄清楚哪一个是最后一行并相应地调整json。
-
the pattern should be compiled separately.
模式应该单独编译。
-
org.json is very inefficient compared to Jackson.
与杰克逊相比,org.json的效率非常低。
#2
1
Check your null value like this
像这样检查你的空值
if(keyvalue == null)
This fixex the issue
这解决了这个问题
#3
0
Firstly, create an model class which is corresponding to the JSON string.
首先,创建一个与JSON字符串对应的模型类。
Add
@JsonIgnoreProperties(ignoreUnknown = true)
to your model class such as
到您的模型类,如
@JsonIgnoreProperties(ignoreUnknown = true)
public class Video {
//the properties
}
http://www.baeldung.com/jackson-deserialize-json-unknown-properties
#4
0
Use Jackson API and use @JsonInclude(Include.NON_NULL) on your model/DTO class to remove.
使用Jackson API并在模型/ DTO类上使用@JsonInclude(Include.NON_NULL)进行删除。
Like
@JsonInclude(Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
public class Video {
//the properties
}