Android JSON Parsing made easy using Jackson!
Ever wanted to pass JSON format data from web services….well, we know things cant get tricky and everybody hates complicated solutions! Well, why not give Jackson – a JSON parser a try, it has some pretty neat features…especially if you’re lazy like me!
More info: http://jackson.codehaus.org/
Required Downloads:
jackson-core-asl-1.8.0.jar
jackson-jaxrs-1.8.0.jar
jackson-mapper-asl-1.8.0.jar
jackson-mrbean-1.8.0.jar
jackson-xc-1.8.0.jar
File: recipes-all.json
{ "recipes": [ { "name":"Recipe 1", "id":"8aecfd9b2fa26e83012fa298c2a50017", "recipe":"1 Lorem ipsum...", "image":"/malibu-server/recipe/getImage/8aecfd9b2fa26e83012fa298c2a50017" }, { "name":"Recipe 2", "id":"8aecfd9b2fa26e83012fa298c2a90018", "recipe":"2 Lorem ipsum...", "image": "/malibu-server/recipe/getImage/8aecfd9b2fa26e83012fa298c2a90018" }, { "name": "Recipe 3", "id":"8aecfd9b2fa26e83012fa298c2ae0019", "recipe":"3 Lorem ipsum...", "image": "/malibu-server/recipe/getImage/8aecfd9b2fa26e83012fa298c2ae0019" } ] }
File: main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/textview" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
The Data Model:
File: Recipe.java (the data model entity: singular)
public class Recipe { public String name; public String id; public String recipe; public String image; }
File: Recipes.java (plural)
import java.util.ArrayList; import java.util.HashMap; public class Recipes extends HashMap<String, ArrayList<Recipe>> { //empty }
File: Main.java
public class Main extends Activity { private static final String tag = Main.class.getName(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(com.android.jackson.json.R.layout.main); TextView textview = (TextView) this.findViewById(com.android.jackson.json.R.id.textview); StringBuffer strBuffer = new StringBuffer(); MockRecipesController mockRecipesController = new MockRecipesController(); mockRecipesController.init(); for (Recipe recipe : mockRecipesController.findAll()) { Log.d(tag, "Name: " + recipe.name); Log.d(tag, "ID: " + recipe.id); Log.d(tag, "Recipe: " + recipe.recipe); Log.d(tag, "Image: " + recipe.image); strBuffer.append("Name: " + recipe.name + "\n"); strBuffer.append("ID: " + recipe.id + "\n"); strBuffer.append("Recipe: " + recipe.recipe + "\n"); strBuffer.append("Image: " + recipe.image + "\n"); } // Finally textview.setText(strBuffer.toString()); } } }
File: MockRecipesController.java
package com.malibu.mock; import java.io.IOException; import java.util.ArrayList; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.map.ObjectMapper; import com.malibu.models.Recipe; import com.malibu.models.Recipes; public class MockRecipesController { private final String json = "{ \"recipes\": \n" + " [ \n" + " {\n" + " \"name\":\"Recipe 1\",\n" + " \"id\":\"8aecfd9b2fa26e83012fa298c2a50017\",\n" + " \"recipe\":\"1 Lorem ipsum...\",\n" + " \"image\":\"/malibu-server/recipe/getImage/8aecfd9b2fa26e83012fa298c2a50017\"\n" + " }, \n" + " { \n" + " \"name\":\"Recipe 2\",\n" + " \"id\":\"8aecfd9b2fa26e83012fa298c2a90018\",\n" + " \"recipe\":\"2 Lorem ipsum...\",\n" + " \"image\": \"/malibu-server/recipe/getImage/8aecfd9b2fa26e83012fa298c2a90018\"\n" + " },\n" + " {\n" + " \"name\": \"Recipe 3\",\n" + " \"id\":\"8aecfd9b2fa26e83012fa298c2ae0019\",\n" + " \"recipe\":\"3 Lorem ipsum...\",\n" + " \"image\": \"/malibu-server/recipe/getImage/8aecfd9b2fa26e83012fa298c2ae0019\"\n" + " } \n" + " ]\n" + "}\n" + ""; private ObjectMapper objectMapper = null; private JsonFactory jsonFactory = null; private JsonParser jp = null; private ArrayList<Recipe> recipes = null; private Recipes mRecipes = null; public MockRecipesController() { objectMapper = new ObjectMapper(); jsonFactory = new JsonFactory(); } public void init() { try { jp = jsonFactory.createJsonParser(json); mRecipes = objectMapper.readValue(jp, Recipes.class); recipes = mRecipes.get("recipes"); } catch (JsonParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public ArrayList<Recipe> findAll() { return recipes; } public Recipe findById(int id) { return recipes.get(id); } }
How do you do the inverse, create json string with the object…
http://wiki.fasterxml.com/JacksonInFiveMinutes#Full_Data_Binding_.28POJO.29_Example
1. Create your Model eg. User.java ( from the link above)
2. Create an ObjectMapper
1 ObjectMapper mapper = new ObjectMapper();
2 mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
3 String json = mapper.writeValueAsString(new MyBean()); //User
Hi there! Thanks for the reply, i still have some questions if you dont mind, im using jackson with android and i want to send and receive json content from a webservice. Regarding your original example do you really need to use jsonfactory object?why not this:
String userDataJSON = “{\”userId\”:\”100\”,\”userName\”:{\”firstname\”:\”K\”,\”middlename\”:\”Siva\”,\”lastname\”:\”Prasad\”},\”dob\”:1300878089906}”;
User userFromJSON = mapper.readValue(userDataJSON, User.class);
System.out.println(userFromJSON);
Also how do you deal with root values? if i receive a json string with the root value, i get the unrecognized property error.
regards,
Neat.thanx man..:)
example contains error.
recipes = mRecipes.get(“recipes”);
1 Pingback
GOOGLE
Recent Posts
Archives
Categories
Meta