tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 JSON > JSON JAVA > Null Values

Null Values 

JSON (JavaScript Object Notation) is a lightweight text-based open standard designed for human-readable data interchange. Douglas Crockford has provided a reference implementation of JSON in Java at http://json.org/ useful for JSON serialization and deserialization. Click here to download the compiled library. The following example shows serializing and de-serializing null values .

File Name  :  
com/bethecoder/tutorials/json_java/tests/NullValues.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.json_java.tests;

import org.json.JSONException;
import org.json.JSONObject;

public class NullValues {

  /**
   @param args
   @throws JSONException 
   */
  public static void main(String[] argsthrows JSONException {

    JSONObject obj = new JSONObject(
        "{ \"message1\" : \"null\",  \"message2\" : null }");
    
    System.out.println(obj);
    System.out.println(obj.isNull("message1"));  //false - string 'null'
    System.out.println(obj.isNull("message2"));  //true  - actual null value
    
    JSONObject obj2 = new JSONObject();
    obj2.put("message1""null");
    obj2.put("message2", JSONObject.NULL)//java script null value
    System.out.println(obj2);
    
  }

}
   

It gives the following output,
{"message1":"null","message2":null}
false
true
{"message1":"null","message2":null}



 
  


  
bl  br