| Refresh | Home EGTry.com

parse string to generical JSON object


json sample data

{
  "version": 1,
  "name": "test1"
}


parse json string, and extract a particular value

JSONValue json=JSONParser.parseStrict(content);
JSONObject map=json.isObject();
JSONValue versionObj=map.get("version");
JSONNumber version=versionObj.isNumber();
Window.alert("version number: "+version.doubleValue());


iteration of each key and value

JSONValue json=JSONParser.parseStrict(content);
JSONObject map=json.isObject();
Iterator<String> ite=map.keySet().iterator();
StringBuffer buff=new StringBuffer();
while(ite.hasNext()) {
  String key=ite.next();
  JSONValue value=map.get(key);
  buff.append(key+"="+value.toString()+"\n");
}
Window.alert(buff.toString());


To use JSONParser, add JSON in the module

   <inherits name='com.google.gwt.json.JSON'/>