Im working with an android app where i receive a json based response from a webservice. All was fine when the webservice returned more than one record in response as json array, the issue started when the web service started sending data which had only one record and it was sent as json object.On the client side i cant predict the number of records being returned(like 1 or more than 1) i want to find a solution which accommodates both conditions. The below code is the one i use to aprse the data when there are more than one records,
get_response = new JSONObject(webservice_out);
JSONArray inventory_data = get_response.getJSONArray("inventorydata");
Log.e("inventory", inventory_data.toString());
for (int j = 0; j < inventory_data.length(); j++)
{
JSONObject e1 = inventory_data.getJSONObject(j);
Log.e("names", e1.getString("itemName"));
JSONObject e3 = e1.getJSONObject("passenger");
}
In the above code "inventorydata" is one of the json value returned as a response from the webservice, whenever "inventorydata" hold only one record in response its being sent as json object, when it has more than 1 records its being sent as json array. Since the number of records in the response are dynamic , i want to find a solution which can hold both (json object and array) depending the response from the webservie.
Note: i dont have any authority to make changes to the webservice