user3885363
user3885363

Reputation: 1

java.lang.String cannot be converted to JSONObject. mismatch type

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:

Upvotes: 0

Views: 98

Answers (4)

Sachin Suthar
Sachin Suthar

Reputation: 692

@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());


        }

Upvotes: 1

Sunil Sunny
Sunil Sunny

Reputation: 3994

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

Upvotes: 0

kgundula
kgundula

Reputation: 322

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

Upvotes: 0

Junaid Hafeez
Junaid Hafeez

Reputation: 1616

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.

Upvotes: 0

Related Questions