-1

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

HugoRamos
  • 3
  • 1
  • 6
  • 1
    Show what have you tried to achieve it – intrepidkarthi Jul 28 '14 at 18:41
  • 2
    Create a class that represents the json data structure and then parse it with something like Gson - https://sites.google.com/site/gson/gson-user-guide – Willie Nel Jul 28 '14 at 18:44
  • i create a json object that reads my first node, then i create an array to read config and i iterate that array with a for structure, sorry for my english – HugoRamos Jul 28 '14 at 18:44
  • I second @Willie Nel on using a library. I made what I would consider as a mistake by manually parsing all of my JSON for a while, things like GSON make things exponentially easier (IMO). – zgc7009 Jul 28 '14 at 18:49
  • first you should get all the json data into a single String, then using `final JSONObject json = new JSONObject(json)` you can reference the object and call `JSONArray myeArray = json.getJSONArray("product")` – Branky Jul 28 '14 at 18:54

2 Answers2

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.

Cruceo
  • 6,763
  • 2
  • 31
  • 52
  • Thank you so much for your answer @Guardanis i forgot to add my first JSONObject "innerConfigObj" to my other iteration, thank you this works !!!! – HugoRamos Jul 28 '14 at 19:32
0

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;
        }
    }
}
Willie Nel
  • 363
  • 2
  • 9