Skip to content

Instantly share code, notes, and snippets.

@ryanjbaxter
Created August 4, 2011 01:44
Show Gist options
  • Save ryanjbaxter/554a2fd80015b3f199a1 to your computer and use it in GitHub Desktop.
Save ryanjbaxter/554a2fd80015b3f199a1 to your computer and use it in GitHub Desktop.
Using JSON APIs In Notes
public class JSONDemo {
private String myJsonString;
/**
* Constructor
* @param myJsonString a string representation of JSON
*/
public JSONDemo(String myJsonString) {
this.myJsonString = myJsonString;
}
/*
* Gets a Java object which you can use to get values from the JSON.
* @return a Java object which you can use to get values from the JSON
*/
public JsonJavaObject getJson() {
try {
//Use the extended instance so we can get back a JsonJavaObject from JsonParser
JsonFactory jsonFactory = JsonJavaFactory.instanceEx;
Object obj = JsonParser.fromJson(jsonFactory, myJsonString);
if(obj instanceof JsonJavaObject){
JsonJavaObject jsonObject = (JsonJavaObject)obj;
return jsonObject
}
} catch (JsonException e) {
e.printStackTrace();
} catch (TokenMgrError e){
e.printStackTrace();
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment