ENCODE AND DECODE JSON TEXT FROM JAVA TO JAVASCRIPT – USING MAP

JSON.simple is a simple Java toolkit for JSON. You can use JSON.simple to encode or decode JSON text.

  1. Mapping between JSON and JAVA entities:
    JSON Java
    object java.util.Map
  2. Example:
    //import org.json.simple.JSONValue;
    Encode a java.util.List in Java
    Java:

      Map<String, String> obj = new LinkedHashMap()<String, String>;
      obj.put("name","foo");
      obj.put("num",new Integer(100));
      obj.put("balance",new Double(1000.21));
      obj.put("is_vip",new Boolean(true));
      obj.put("nickname",null);
    
      String jsonText = JSONValue.toJSONString(obj);

    Decode it in JavaScript using LinkedHashMap

    JavaScript:

      var js_obj = JQuery.parseJSON(jsonText);
      // or var js_obj = JSON.parse(jsonText);
      var name = js_obj[name];
      var num = js_obj[num];
    
      // to see if key exists
      var key = "name";
      if( key in js_obj )

Reference Link: https://code.google.com/p/json-simple/
Reference Link: http://api.jquery.com/jQuery.parseJSON/
Reference Link: http://www.jquerysdk.com/api/jQuery.toJSON

One thought on “ENCODE AND DECODE JSON TEXT FROM JAVA TO JAVASCRIPT – USING MAP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s