0

JSON Array

 [
  {
    "0": {
      "program_name": "Daycare"
    },
    "1": {
      "program_name": "Preschool"
    },
    "program_name": [
      {
        "program_name": "Daycare"
      },
      {
        "program_name": "Preschool"
      }
    ],
    "batch_name": [
      {
        "0": "3 Hours",
        "batch_class_name": "3 Hours"
      },
      {
        "0": "5 Hours",
        "batch_class_name": "5 Hours"
      }
    ]
  }
]

This is what I've done so far: -

void getProgram() {

    progressDialog = new MaterialDialog.Builder(getActivity())
            .content("Please wait....")
            .progress(true, 0)
            .show();
    StringRequest stringRequest = new StringRequest(Request.Method.POST, GlobalConfig.GET_PROGRAM_AND_BATCH_OF_TEACHER,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    progressDialog.dismiss();
                    Log.e("response", response);
                    try {



                        JSONArray jsonArray = new JSONArray(response);
                        JSONObject jsonObject = jsonArray.getJSONObject(0);
                        JSONArray jsonProgramArray = jsonObject.getJSONArray("program_name");
                        for (int i = 0; i < jsonObject.length() - 1; i++) {

                            BatchModel programe = new BatchModel();
                            programe.setTitle(jsonProgramArray.getString(i));
                            programe.setId(jsonProgramArray.getString(i));
                            programlist.add(programe);
                            Log.e("Program test", programlist.toString());


                        }
                    } catch (JSONException e) {
                        e.printStackTrace();

I want to add to list strings of "program_name" which is mark in bold:

But I'm getting this error:

1
  • JSONObject jsonObject = jsonArray.getJSONObject("0"); Commented Jan 19, 2017 at 6:45

4 Answers 4

1

@user3885363 .you try this.

 JSONArray jsonArray = new JSONArray(response);
        JSONObject jsonObject = jsonArray.getJSONObject(0);
        JSONArray jsonProgramArray = jsonObject.getJSONArray("program_name");

        for (int i = 0; i < jsonProgramArray.length(); i++) {
            JSONObject jsonObjectnew = jsonProgramArray.getJSONObject(i);
            BatchModel programe = new BatchModel();
            programe.setTitle(jsonObjectnew.getString("program_name"));
            programlist.add(programe);
            Log.e("Program test", programlist.toString());


        }
Sign up to request clarification or add additional context in comments.

1 Comment

Accept answer bro so other person get idea easily.
0

you are using jsonObject.length() in your loop for (int i = 0; i < jsonObject.length() - 1; i++) but you have your program_name in the array you just get from object. JSONArray jsonProgramArray = jsonObject.getJSONArray("program_name");. Try to loop on jsonProgramArray and get the program_name object for each program.

Comments

0
JSONArray jsonArray = new JSONArray(response);

// Here you are getting the program JSONArray
JSONArray jsonProgramArray = jsonArray.getJSONArray("program_name");

for (int i = 0; i < jsonProgramArray.length() - 1; i++) {
    // Get the each Json Object from the Array
    JSONObject jsonProgramObject = jsonProgramArray.getJSONObject(i);

    String program_name = jsonProgramObject.getString("program_name") 
}

You can do the same thing for the other batch_name array

Comments

0

You have 2 "program_name" one is string inside jsonObject "0" to get that you have to do like this

 JSONArray jsonArray = new JSONArray(response);
 JSONObject jsonObject = jsonArray.getJSONObject(0);

 JSONObject jsonObjt_0 = jsonObject.getJSONObject("0");
 String productName = jsonObjt_0.getString("program_name")

Then you have a jsonArray named "program_name" to get that do like this.

JSONArray productArray =  jsonObject.getJSONArray("program_name");
 for (int i = 0; i < productArray .length(); i++) {
   JSONObject listItem = productArray.getJSONObject(i);
   BatchModel programe = new BatchModel();
   programe.setTitle(listItem.getString("program_name"));
   programe.setId(listItem.getString("program_name"));
   programlist.add(programe);
}

PS. This is a weird json, consider changing the json with some meanigful names,Please don't use the same name for everything..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.