How to parse this nested JSON array in android
Here is how I think your JSON Parser should look like (there can be some typo mistakes,I didn't test this code on editor : )) :
JSONObject mainObj = new JSONOBject(myString);
if(mainObj != null){
JSONArray list = mainObj.getJSONArray("prodCat_list");
if(list != null){
for(int i = 0; i < list.length();i++){
JSONObject elem = list.getJSONObject(i);
if(elem != null){
JSONArray prods = elem.getJSONArray("prods");
if(prods != null){
for(int j = 0; j < prods.length();j++){
JSONObject innerElem = prods.getJSONObject(j);
if(innerElem != null){
int cat_id = innerELem.getInt("cat_id");
int pos = innerElem.getInt("position");
String sku = innerElem.getString("sku");
}
}
}
}
}
}
}
How to get data from nested json array android
Use list made changes in your class copy and generate getter setter
ChallengeModel
public class ChallengeModel {
private String ChallengeID;
private String ChallengeDate;
private String ChallengeName;
private String ChallengeIcon;
private String BetID;
private List<ChallengeItem> challengeItem;
//TODO
//Generate Getter and setter
}
ChallengeItem
public class ChallengeItem {
private String ChallengeItemID;
private String ChallengeID;
private String MatchID;
private String TeamAName;
private String TeamBName;
private String TeamAScore;
private String TeamBScore;
private String MatchDate;
private String MatchTime;
private String Venue;
private String VenuCity;
private String TeamAValue;
private String TeamBValue;
//TODO
//Generate Getter and setter
}
ChallengeJsonConvert
public class ChallengeJsonConvert {
public static List<ChallengeModel> challengesRequest(String content) {
try {
JSONArray challengesArray = new JSONArray(content);
List<ChallengeModel> challengeModelList = new ArrayList<>();
for (int i = 0; i < challengesArray.length(); i++) {
JSONObject obj = challengesArray.getJSONObject(i);
ChallengeModel challenge = new ChallengeModel();
challenge.setBetID(obj.getString("BetID"));
challenge.setChallengeDate(obj.getString("ChallengeDate"));
challenge.setChallengeIcon(obj.getString("ChallengeIcon"));
challenge.setChallengeID(obj.getString("ChallengeID"));
challenge.setChallengeName(obj.getString("ChallengeName"));
JSONArray challengeItemArray = obj.getJSONArray("challengeItem");
for (int j = 0; j < challengeItemArray.length(); j++) {
JSONObject challengeObject = challengeItemArray.getJSONObject(j);
ChallengeItem challengeItem = new ChallengeItem();
challengeItem.setChallengeItemID(challengeObject.getString("CallengeItemID"));
//TODO
//Get other object and set into challengeItem
challenge.getChallengeItem().add(challengeItem);
}
challengeModelList.add(challenge);
}
return challengeModelList;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
}
Iterate and parse nested JSON Objects with Android VOLLEY
I'm going to make these strings member variables, but for now, I created them in my onResponse
method. I was almost there. I just needed to iterate over the final JSONArray to get the values.
String serve_mode = "";
String address = "";
String label = "";
String weight = "";
try {
JSONObject data = response.getJSONObject("data");
JSONArray region = data.getJSONArray("region");
JSONObject pool = region.getJSONObject(0);
JSONArray item = pool.getJSONArray("pool");
//NOW ITERATE OVER THE NESTED ARRAY "pool"
for(int i = 0; i < item.length(); i++) {
JSONObject pool_item = item.getJSONObject(i);
serve_mode = pool_item.getString("serve_mode");
address = pool_item.getString("address");
label = pool_item.getString("label");
weight = pool_item.getString("weight");
Log.d(TAG, "serve_mode: "+serve_mode);
Log.d(TAG, "address: "+address);
Log.d(TAG, "label: "+label);
Log.d(TAG, "weight: "+weight);
}
} catch (JSONException e) {
Log.d(TAG, "JSONException: "+e.toString());
}
This yields (address and label fields were edited):
D/MainActivity: serve_mode: obey
D/MainActivity: address: 12.345.678.90
D/MainActivity: label: SomeCompany A
D/MainActivity: weight: 2
D/MainActivity: serve_mode: obey
D/MainActivity: address: 123.456.789.01
D/MainActivity: label: SomeCompany B
D/MainActivity: weight: 1
D/MainActivity: serve_mode: obey
D/MainActivity: address: 456.789.012.34
D/MainActivity: label: SomeCompany C
D/MainActivity: weight: 2
Using Kotlin to parse nested json array/object
You must first get JSONArray from the object according to the following code and then access the class_name variable
val obj = JSONObject(js)
val jsonArray = obj.getJSONArray("childScoresList")
for (i in 0 until jsonArray.length()) {
val classes = jsonArray.getJSONObject(i).getJSONArray("classes")
for (x in 0 until classes.length()) {
val categoryName = classes.getJSONObject(x).getString("category_name")
val className = classes.getJSONObject(x).getString("class_name")
}
}
How to parse nested JSON array?
Try this,
try {
JSONObject mainObj = new JSONObject(sb.toString());
JSONArray question_data = null;
question_data = mainObj.getJSONArray("question_data");
for (int i = 0; i < question_data.length(); i++) {
JSONObject elem = question_data.getJSONObject(i);
JSONArray questions = elem.getJSONArray("question_list");
for (int k = 0; k < questions.length(); k++) {
JSONObject queandopt = questions.getJSONObject(k);
JSONObject question_obj=queandopt.getJSONObject("question");
String question_id_value = question_obj.getString("question_id");
String question_value = question_obj.getString("question");
String question_mar_value = question_obj.getString("question_mar");
String question_hindi_value = question_obj.getString("question_hindi");
Log.d("TAG","question_id_value"+question_id_value+"question_value"+question_value+"question_mar_value"+question_mar_value+"question_hindi_value"+question_hindi_value);
JSONArray option_list = queandopt.getJSONArray("option");
for (int j = 0; j < option_list.length(); j++) {
JSONObject option = option_list.getJSONObject(j);
String option_id = option.getString("option_id");
String option_desc = option.getString("option");
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
how to parse Nested JSONArray in android
you need to create new jsonContent object in every loop because currently you are just changing the values of same object in every iteration and adding it to the list.
jsonContent jsonContent=null; //my class
JSONArray posts = jsonObj.getJSONArray("posts");
for (int i = 0; i < posts.length(); i++) {
jsonContent=new jsonContent(); //my class
// ^^^^^^^^ create new object
JSONObject c = posts.getJSONObject(i);
jsonContent.title=c.getString("title");
JSONArray attachments=c.getJSONArray("attachments");
for (int j=0;j<attachments.length();j++)
{
JSONObject a=attachments.getJSONObject(j);
jsonContent.imgurl=a.getString("url");
}
listcontent.add(jsonContent); //mylist
}
Improvement : use getter and setter function instead of exposing your class fields as public
How to parse nested json array in android using volley library
Here is your problem solution, use below code
try {
JSONObject jsonObject = new JSONObject("response");
JSONArray jsonArray = jsonObject.getJSONArray("City");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
String name = object.getString("name");
if (object.length() != 0) {
Iterator<String> key = object.keys();
while (key.hasNext()) {
String cityname = key.next();
JSONArray ja = object.getJSONArray(cityname);
for (int j = 0; j < ja.length(); j++) {
JSONObject object1 = ja.getJSONObject(j);
String area = object1.getString("area");
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
And try to make your JSON format same.
Related Topics
The Import Android.Support.V7 Cannot Be Resolved
Android: How to Change Checkbox Size
Tabhost with Fragments and Fragmentactivity
How to Limit the Height of Spinner Drop Down View in Android
Getting the Battery Current Values for the Android Phone
Why Do I Get Com.Google.Android.Gms.Common.Api.Apiexception: 10:
Android - R Cannot Be Resolved to a Variable
Is There a Simple Example of the Popupwindow Class Using Android V2.0
"Canvas: Trying to Draw Too Large Bitmap" When Android N Display Size Set Larger Than Small
Android Intent for Playing Video
How to Use Radiogroup in Listview Custom Adapter
How to Get Android Device Screen Size
How to Change Focus Color of Edittext in Android
Make an Android Button Change Background on Click Through Xml
Adb Server Version (36) Doesn't Match This Client (39); Killing
How to Change the Size of a Switch Widget
Android: Background Image Size (In Pixel) Which Support All Devices