JSON4J JavaTM usage examples

The core object, JSONObject, is an extension of HashMap. This object has two capabilities for handling JSON.  The first is a static function that parses a JSON string and returns a JSONObject instance that represents that particular string.  The second function is to construct an entirely new JSON string by allocating a new JSONObject instance, then using the get, put, and remove methods to manipulate the attributes.  The PUT function of the JSONObject does type checking to ensure that objects in the JSONObject instance have corresponding JSON representations.  Current types that can be put into or fetched through get from the map are as follows:

    java.lang.String
    java.lang.Boolean
    java.lang.Number
    com.ibm.json.java.JSONArray
    com.ibm.json.java.JSONObject
    null

You can then serialize any constructed JSONObject instance to a JSON string by using of one of several serialize methods.  With these methods, you can specify an output stream, an output writer, or return a JavaTM string of the JSON text, and indicate whether to use a verbose (formatted and indented for improved readability), or compact (single line, transmission efficient) format. See the following usage examples:

Usage examples:
Demo 1:
public void demoJson()
{
   String JSON = "{\"attribute\":\"foo\", \"number\":100.959}";
      try
      {
         JSONObject obj = (JSONObject)JSON.parse(JSON);
         Double dbl = (Double)obj.get("number");
         if (dbl == null || dbl.doubleValue() != 100.959)
         {
            throw new Exception("Numeric value was incorrect");
         }

         String str = (String)obj.get("attribute");
         if (dbl == null || !str.equals("foo"))
         {
            throw new Exception("String attribute was incorrect");
         }
         String jsonStr = obj.serialize(true);
         System.out.println(jsonStr);
      }
      catch (Exception ex)
      {
         ex.printStackTrace();
      }
}

     The output of the previous example is:
   {
     "attribute": "foo",
     "number": 100.959
   }


Demo 2:
public void demoJson()
{
   String JSON = "{\"attribute\":\"foo\", \"number\":100.959}";
      try
      {
         JSONObject obj = JSONObject.parse(JSON);
         Double dbl = (Double)obj.get("number");
         if (dbl == null || dbl.doubleValue() != 100.959)
         {
            throw new Exception("Numeric value was incorrect");
         }

         String str = (String)obj.get("attribute");
         if (dbl == null || !str.equals("foo"))
         {
            throw new Exception("String attribute was incorrect");
         }
         String jsonStr = obj.serialize(true);
         System.out.println(jsonStr);
      }
      catch (Exception ex)
      {
         ex.printStackTrace();
      }
}

     The output of the previous example is:
   {
     "attribute": "foo",
     "number": 100.959
   }


Demo 3:
public void demoJson2()
{
   try
   {
      JSONObject obj = new JSONObject();
      obj.put("attribute", "foo");
      obj.put("number", new Double(100.959));
      String jsonStr = obj.serialize(true);
      System.out.println(jsonStr);
   }
   catch (Exception ex)
   {
      ex.printStackTrace();
   }
}

     The output of the previous example is:
   {
     "attribute": "foo",
     "number": 100.959
   }

Refer to specific API documentation for additional details.


Terms of Use | Feedback