Android Read Jsonarray Into A Jsonarray
How can i read a json array that contains a json array this is my json { 'product': { 'name': 'myApp', 'config': [ { 'grade': 'eleme
Solution 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:
Strings= your_json_data;
Now, to parse that:
JSONObject jsonObj = newJSONObject(s);
JSONObject productJson = jsonObject.getJSONObject("product"); // May want to consider using optJSONObject for null checking in case your key/value combination doesn't existString 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.
Solution 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;
publicclassMainActivityextendsActivity {
privatestatic final StringTAG = MainActivity.class.getSimpleName();
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String json = "{\"product\": {\"name\": \"myApp\",\"config\": [{\"grade\": \"elementary school\",\"courses\": [{\"name\": \"Math\",\"teacher\": \"David\"}]}]}}";
JsonParseResult result = newGson().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());
}
}
}
publicclassJsonParseResult {
privateProduct product;
publicJsonParseResult(Product product) {
this.product = product;
}
publicProductgetProduct() {
return product;
}
publicvoidsetProduct(Product product) {
this.product = product;
}
}
publicclassProduct {
privateString name;
privateList<Config> config;
publicProduct(String name, List<Config> config) {
this.name = name;
this.config = config;
}
publicStringgetName() {
return name;
}
publicvoidsetName(String name) {
this.name = name;
}
publicList<Config> getConfig() {
return config;
}
publicvoidsetConfig(List<Config> config) {
this.config = config;
}
}
publicclassConfig {
privateString grade;
privateList<Course> courses;
publicConfig(String grade, List<Course> courses) {
this.grade = grade;
this.courses = courses;
}
publicStringgetGrade() {
return grade;
}
publicvoidsetGrade(String grade) {
this.grade = grade;
}
publicList<Course> getCourses() {
return courses;
}
publicvoidsetCourses(List<Course> courses) {
this.courses = courses;
}
}
publicclassCourse {
privateString name;
privateString teacher;
publicCourse(String name, String teacher) {
this.name = name;
this.teacher = teacher;
}
publicStringgetName() {
return name;
}
publicvoidsetName(String name) {
this.name = name;
}
publicStringgetTeacher() {
return teacher;
}
publicvoidsetTeacher(String teacher) {
this.teacher = teacher;
}
}
}
Post a Comment for "Android Read Jsonarray Into A Jsonarray"