Skip to content

Instantly share code, notes, and snippets.

@prb112
Created March 15, 2014 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prb112/9575448 to your computer and use it in GitHub Desktop.
Save prb112/9575448 to your computer and use it in GitHub Desktop.
package json.demo;
import com.ibm.commons.util.io.json.JsonException;
import com.ibm.commons.util.io.json.JsonJavaFactory;
import com.ibm.commons.util.io.json.JsonJavaObject;
import com.ibm.commons.util.io.json.JsonParser;
import com.ibm.sbt.services.client.base.datahandlers.JsonDataHandler;
public class Main {
public static void main(String[] args) {
try {
String jsonString = "{ \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": {\"title\": \"S\",\"GlossList\": {\"GlossEntry\": {\"ID\": \"SGML\",\"SortAs\": \"SGML\",\"GlossTerm\": \"Standard Generalized Markup Language\",\"Acronym\": \"SGML\",\"Abbrev\": \"ISO 8879:1986\",\"GlossDef\": {\"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\"GlossSeeAlso\": [\"GML\", \"XML\"]},\"GlossSee\": \"markup\" } }} } }";
/**
* Creates the Java Representation
*/
JsonJavaObject jsonObject = (JsonJavaObject) JsonParser.fromJson(
JsonJavaFactory.instanceEx, jsonString);
/**
* Processing it requires a handler
*/
JsonDataHandler handler = new JsonDataHandler();
handler.setData(jsonObject);
/**
* Select a JsonJavaObject that's a Child Object
*/
JsonJavaObject entryJson = handler.getEntry("glossary");
/**
* Get a Specific Entry and have it cast to a preferred object type
*/
String title = entryJson.getAsString("title");
System.out.println(title);
} catch (JsonException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment