Created
October 26, 2010 00:59
-
-
Save shirou/646118 to your computer and use it in GitHub Desktop.
Jackson JSON Stream parser example
This file contains hidden or 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
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