Created
March 15, 2014 23:21
-
-
Save prb112/9575448 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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