Skip to content

Instantly share code, notes, and snippets.

@shirou
Created October 26, 2010 00:59
Show Gist options
  • Save shirou/646118 to your computer and use it in GitHub Desktop.
Save shirou/646118 to your computer and use it in GitHub Desktop.
Jackson JSON Stream parser example
JsonFactory fac = Util.getJsonFactory(); // recycle JsonFactory
// JsonFactory fac = new JsonFactory(); // or just use once.
JsonParser jp = null;
try {
jp = fac.createJsonParser(json);
jp.nextValue(); // responseData
jp.nextValue(); // null
jp.nextValue(); // results
while(jp.nextValue() != JsonToken.END_ARRAY){
TestEntryClass ent=null;
ent = new TestEntryClass();
while (jp.nextToken() != JsonToken.END_OBJECT) {
String fieldName = jp.getCurrentName();
jp.nextToken();
if (fieldName.equals("url")){
ent.url = jp.getText();
}else if (fieldName.equals("title")){
ent.title = jp.getText();
}else if (fieldName.equals("titleNoFormatting")){
ent.titleNoFormatting = jp.getText();
}else if (fieldName.equals("content")){
ent.content = jp.getText();
}
}
if (ent != null){
list.add(ent);
}
}
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if (jp != null){
try {
jp.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment