Here is an example of how you would parse it using gson - https://code.google.com/p/google-gson/. It really makes life a lot easier, you create your class structure once and then just reuse it throughout your application.
package com.example.jsonparse;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.google.gson.Gson;
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String json = "{\"product\": {\"name\": \"myApp\",\"config\": [{\"grade\": \"elementary school\",\"courses\": [{\"name\": \"Math\",\"teacher\": \"David\"}]}]}}";
JsonParseResult result = new Gson().fromJson(json, JsonParseResult.class);
for (Config config : result.getProduct().getConfig()) {
Log.d(TAG, "Courses for grade: " + config.getGrade());
for (Course course : config.getCourses()) {
Log.d(TAG, "Course Name: " + course.getName());
Log.d(TAG, "Course Teacher: " + course.getTeacher());
}
}
}
public class JsonParseResult {
private Product product;
public JsonParseResult(Product product) {
this.product = product;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
}
public class Product {
private String name;
private List config;
public Product(String name, List config) {
this.name = name;
this.config = config;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getConfig() {
return config;
}
public void setConfig(List config) {
this.config = config;
}
}
public class Config {
private String grade;
private List courses;
public Config(String grade, List courses) {
this.grade = grade;
this.courses = courses;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public List getCourses() {
return courses;
}
public void setCourses(List courses) {
this.courses = courses;
}
}
public class Course {
private String name;
private String teacher;
public Course(String name, String teacher) {
this.name = name;
this.teacher = teacher;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
}
}