Android将JSONArray读入JSONArray

时间:2014-07-28 18:40:11

标签: android json eclipse arrays tap

我如何读取包含json数组的json数组,这是我的json

{
    "product": {
        "name": "myApp",
        "config": [
            {
                "grade": "elementary school",
                "courses": [
                    {
                        "name": "Math",
                        "teacher": "David"
                    }
                ]
            }
        ]
    }
}

例如我如何阅读" config"然后是课程,生成一个列表,告诉我小学,然后如果我点击该名称我的应用程序显示课程的名称和老师的名字

2 个答案:

答案 0 :(得分:1)

首先,这不是JSONArray;它是一个JSONObject。 JSONArray表示为打开和关闭大括号(分别为[和]),而JSONObject由开/关括号(分别为{和})表示。

现在,回答你关于如何解析它的问题......

我们假设你有:

String s = your_json_data;

现在,解析一下:

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

现在这应该让你开始基本的东西......让我们重复一遍实际的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");
    }
}

这应该允许你迭代它们。

答案 1 :(得分:0)

以下是使用gson - https://code.google.com/p/google-gson/解析它的示例。它确实让生活变得更轻松,您只需创建一次类结构,然后在整个应用程序中重复使用它。

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;
        }
    }
}