问题
How can i read a json array that contains a json array this is my json
{
"product": {
"name": "myApp",
"config": [
{
"grade": "elementary school",
"courses": [
{
"name": "Math",
"teacher": "David"
}
]
}
]
}
}
for example how can i read "config" and then courses, to generate a list that show me elementary school and then if i tap in that name my app show me the name of the course and the name of the teacher
回答1:
Well first, this is not a JSONArray; it's a JSONObject. A JSONArray is denoted by opening and closes braces ([ and ], respectively), while a JSONObject is denoted by opening/closing brackets ({ and }, respectively).
Now, to answer your question as to how to parse it...
Let's assume you have:
String s = your_json_data;
Now, to parse that:
JSONObject jsonObj = new JSONObject(s);
JSONObject productJson = jsonObject.getJSONObject("product"); // May want to consider using optJSONObject for null checking in case your key/value combination doesn't exist
String name = productJson.getString("name"); // myApp
Now that should get you started with the basic stuff... Let's go over iterating through an actual JSONArray:
JSONArray configJsonArray = productJson.getJSONArray("config");
for(int configIterator = 0; configIterator < configJsonArray.length(); configIterator++){
JSONObject innerConfigObj = configJsonArray.getJSONObject(configIterator);
String configGrade = innerConfigObj.getString("grade");
JSONArray courseJsonArray = innerConfigObj.getJSONArray("courses");
for(int courseIterator = 0; courseIterator < courseJsonArray.length(); courseIterator++){
JSONObject innerCourseObj = innerCourseObj.getJSONObject(courseIterator);
String courseName = innerCourseObj.getString("name");
String courseTeacher = innerCourseObj.getString("teacher");
}
}
That should allow you to iterate through them.
回答2:
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> config;
public Product(String name, List<Config> config) {
this.name = name;
this.config = config;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Config> getConfig() {
return config;
}
public void setConfig(List<Config> config) {
this.config = config;
}
}
public class Config {
private String grade;
private List<Course> courses;
public Config(String grade, List<Course> courses) {
this.grade = grade;
this.courses = courses;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> 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;
}
}
}
来源:https://stackoverflow.com/questions/25002030/android-read-jsonarray-into-a-jsonarray