How do you set a value to null with org.json.JSONObject in java?

后端 未结 2 755
走了就别回头了
走了就别回头了 2020-12-01 02:47

How do you set a value to null with org.json.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

相关标签:
2条回答
  • 2020-12-01 02:57

    Try to set JSONObject.NULL instead of null:

    A sentinel value used to explicitly define a name with no value. Unlike null, names with this value:

    • show up in the names() array
    • show up in the keys() iterator
    • return true for has(String)
    • do not throw on get(String)
    • are included in the encoded JSON string.

    This value violates the general contract of equals(Object) by returning true when compared to null. Its toString() method returns "null".

    0 讨论(0)
  • 2020-12-01 03:13

    For me with net.sf.json.JSONObject I need to create a null JSON by

    new JSONObject(true)
    

    This is how I get class into groovy:

    groovy.grape.Grape.grab(group: "org.kohsuke.stapler", module: "json-lib", version: "2.4-jenkins-2")
    
    0 讨论(0)
提交回复
热议问题