Android的读JSONArray成JSONArray [英] Android read JSONArray into a JSONArray

查看:113
本文介绍了Android的读JSONArray成JSONArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能读取包含一个JSON数组,这是我的JSON JSON数组

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

推荐答案

那么首先,这不是一个JSONArray;这是一个JSONObject的。一个JSONArray表示通过打开和关闭括号([和],分别),而一的JSONObject表示通过打开/关闭括号({和},分别)。

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...

让我们假设你有:

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迭代:

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.

这篇关于Android的读JSONArray成JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆