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

JSON Subset 

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 a subset of simple JSON object.

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

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

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

public class JsonSubset {

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

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("name""Sriram");
    map.put("age"2);
    map.put("dob"new Date(11046));
    map.put("hobby""painting");
    
    JSONObject obj = new JSONObject(map);
    System.out.println(obj.toString(2))//pretty print with indent
    
    JSONObject subset = new JSONObject(obj, new String [] { "name""age" });
    System.out.println(subset.toString(2))//pretty print with indent
  }

}
   

It gives the following output,
{
  "name": "Sriram",
  "age": 2,
  "dob": "Thu May 06 00:00:00 IST 2010",
  "hobby": "painting"
}

{
  "age": 2,
  "name": "Sriram"
}



 
  


  
bl  br