-2

I have the sample Json format here and I would like to parse using android volley but i got stuck with the error: at entry of type org.json.JSONObject cannot be converted to JSONArray

    {
  "entry": {
    "": {
      "": "http:\/\/www.w3.org\/2005\/Atom",
      "m": "http:\/\/schemas.microsoft.com\/ado\/2007\/08\/dataservices\/metadata",
      "d": "http:\/\/schemas.microsoft.com\/ado\/2007\/08\/dataservices"
    },
    "FOO_BLOCK": {
      "BAR_xmlbase": "https:\/\/ab.com:443\/dap\/opu\/odata\/dap\/BATCH_SRV\/"
    },
    "id": {
      "TEXT": "https:\/\/ab.com:443\/dap\/opu\/odata\/dap\/BATCH_SRV\/BatchSet('1000')"
    },
    "title": {
      "FOO_BLOCK": {
        "BAR_type": "text"
      },
      "TEXT": "BatchSet('1000')"
    },
    "updated": {
      "TEXT": "2018-06-05T13:45:24Z"
    },
    "category": {
      "FOO_BLOCK": {
        "BAR_term": "BATCH_SRV.Batch",
        "BAR_scheme": "http:\/\/schemas.microsoft.com\/ado\/2007\/08\/dataservices\/scheme"
      },
      "TEXT": "NULL"
    },
    "link": {
      "FOO_BLOCK": {
        "BAR_href": "BatchSet('1000')",
        "BAR_rel": "self",
        "BAR_title": "Batch"
      },
      "TEXT": "NULL"
    },
    "content": {
      "FOO_BLOCK": {
        "BAR_type": "application\/xml"
      },
      "mproperties": {
        "dCharg": {
          "TEXT": 1000
        },
        "dICharg": {
          "TEXT": 1000
        },
        "dMaktx": {
          "TEXT": "No Material description avaibalbe"
        },
        "dStatus": {
          "TEXT": "Batch is unrestricted"
        },
        "dStock": {
          "TEXT": "NULL"
        }
      }
    }
  }
}

Below is the code which i have write in android.

JsonObjectRequest arrReq = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject >() {
                    @Override
                    public void onResponse(JSONObject  response) {
                        Log.e("FD", response.toString());
                        // Check the length of our response (to see if the user has any repos)
                        // Process the JSON
                        try{
                            // Get the JSON array
                            JSONArray jsonArray = response.getJSONArray("entry");
                            Log.e("FD", jsonArray.toString());


                            // Loop through the array elements
                            for(int i=0;i<array.length();i++){
                                // Get current json object
                                JSONObject data = array.getJSONObject(i);


                                String Charg = student.getString("Charg");
                                String Status = student.getString("Status");

                                // Display the formatted json data in text view

                                addToRepoList(Charg , Status );
                            }

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

                    }
                },

                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // If there a HTTP error then add a note to our repo list.
                        setRepoListText("Error while calling REST API");
                        Log.e("Volley", error.toString());
                    }
                }
        );

But i am getting Errors while parsing it in Android. The error is- JSONObject can not be converted to JSONArray

Can anyone please provide sample code to parse this json Array.

user9550188
  • 77
  • 1
  • 2
  • 9
  • Possible duplicate of [How do I parse JSON in Android?](https://stackoverflow.com/questions/9605913/how-do-i-parse-json-in-android) – ADM Jun 05 '18 at 14:45

2 Answers2

1

Your entry is not a JSONOArray but yes a JSONObject. In your json response nothing are a JSONArray

Try something like this:

JSONObject entryObj = response.getJSONObject("entry")

And then:

JSONObject fooBlockObj = entryObj.getJSONObject("FOO_BLOCK")
...
JSONObject contentObj = entryObj.getJSONObject("content")
JSONObject mProperties = contentObj.getJSONObject("mproperties")
JSONObject dCharg = mProperties.getJSONObject("dCharg")
JSONObject dStatus = mProperties.getJSONObject("dStatus")

Finally

String charg = dCharg.getString("TEXT")
String status = dStatus.getString("TEXT")
Abner Escócio
  • 2,697
  • 2
  • 17
  • 36
0

Your json response does not contain a json array. Some of the issues that should be noted are

  • entry is a json object but you are parsing it as an array
  • you are trying to fetch the keys Charg, Status from the json which are not even present in the response (instead dCharg, dStatus are present, which might be the keys you want)

If you need to fetch the keys Charg, Status, check the following

                 try{
                     JSONObject data = response.getJSONObject("entry");
                     JSONObject content = data.getJSONObject("content");
                     JSONObject mproperties = content.getJSONObject("mproperties");
                     JSONObject status = mProperties.getJSONObject("dStatus")
                     JSONObject charg = mProperties.getJSONObject("dCharg")
                     String statusText = status.getString("TEXT")
                     String chargText = charg.getString("TEXT")
                    }

                    }catch (JSONException e){
                        e.printStackTrace();
                    }
Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44