how to get a real null value instead of a JSONObject.NULL value when parsing JSON in grails

后端 未结 3 545
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-19 08:17

I\'m trying to parse some JSON in Grails using the grails.converters.JSON library. I have a field which will contain either a string, or a null value. When I parse the JSON an

相关标签:
3条回答
  • 2021-02-19 08:52

    I think I found a better solution, which consists in overriding the toString() method implementation of the JSONObject.NULL inner class by copying the JSONObject.java file into your Grails src/java project and then changing the implementation to this:

        /**
         * Get the "" string value.
         * @return An empty String "".
         */
        @Override
        public String toString() {
            return "";
        }
    

    Once you restart with this new class in your classpath, the classloader will use your JSONObject class instead of the one packaged in the Grails dependencies.

    Make sure you keep it in the same package as the original.

    For more details you can go here: https://github.com/grails/grails-core/issues/9129

    Hope it helps :-)

    0 讨论(0)
  • 2021-02-19 08:54

    You may find this more useful and natural

        JSONObject.NULL.equals(jsonObj.get("key_name"))
    
    0 讨论(0)
  • 2021-02-19 09:05

    Have a look at: http://grails.1312388.n4.nabble.com/The-groovy-truth-of-JSONObject-Null-td3661040.html

    Ian Roberts mentions a nice trick to make a null check possible:

    JSONObject.NULL.metaClass.asBoolean = {-> false} 
    
    0 讨论(0)
提交回复
热议问题