Menu

Convert string "null" to null value(JSONNull)

Help
Alexey
2007-05-08
2013-04-29
  • Alexey

    Alexey - 2007-05-08

    Hello

    I want to read/write real string value "null"(as name anything), but string "null"(any of different 16 combinations, if use any character case: "NULL", "nUll", "NulL", ...) converts to null (JSONNull object).

    Example code:
    public class Test_JSON extends TestCase {
        public void test() {
            final Map map = new HashMap();
            map.put("key", "null");
            JSONObject jsonObject = new JSONObject(map);
            System.out.println("JSON Object from Map:");
            System.out.println(jsonObject.toString(4));
            String json = "{key1:\"null\"}";
            System.out.println();
            System.out.println("JSON String(source):");
            System.out.println(json);
            jsonObject = new JSONObject(json);
            System.out.println();
            System.out.println("JSON Object:");
            System.out.println(jsonObject.toString(4));
            System.out.println();
            System.out.println("Real object:");
            System.out.println(jsonObject.get("key1").getClass());
        }
    }

    Output:

    JSON Object from Map:
    {"key": null}

    JSON String(source):
    {key1:"null"}

    JSON Object:
    {"key1": null}

    Real object:
    class net.sf.json.JSONNull

    Is this correct behaviour?

    Thanks

     
    • aalmiray

      aalmiray - 2007-05-12

      Alexey,
      The string [null] or any permutation with lower/upper case will case the value to be converted to a JSONNull as you describe.
      Have you tried the following string "'null'" or even "\"null\""?

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.