Shakti Sharma Shakti Sharma -4 years ago 213
Java Question

org.json.JSONException: JSONArray[0] not a string Exception

I am trying to manupulate a JSON string, and I am getting the following exception:


org.json.JSONException: JSONArray[0] not a string.


My JSON is Like:

{"isDataPresentForAdminPosts":true,"avgUserPostsPerDay":11.27,"avgPageResponsesPerDay":4.93,"responseTimeMaxForAdminPosts"
:"< 10 minutes","rrVsUserPostsData":[[169,43.79,0]],"dailyResponded":[3,8,2,14,13,1,1,4,0,2,0,3,0,0,23
],"responseTimeNoOfAdminPosts":[9,0,0,0,0,0,1,0],"generalRR":{"unRespondedByAdminCount":95,"postedByUsersCount"
:169,"respondedRate":43.79,"unRespondedRate":56.21,"respondedByAdminCount":74},"dailyResponseRate":[33
.33,47.06,33.33,77.78,56.52,100,100,50,0,18.18,0,20,0,0,47.92],"responseTimeMaxForUserPosts":"< 10 minutes"
,"isDataPresentForUserPosts":true,"categories":["2016-03-07","2016-03-08","2016-03-09","2016-03-10","2016-03-11"
,"2016-03-12","2016-03-13","2016-03-14","2016-03-15","2016-03-16","2016-03-17","2016-03-18","2016-03-19"
,"2016-03-20","2016-03-21"],"dailyUnresponded":[6,9,4,4,10,0,0,4,4,9,8,12,0,0,25],"responseTimeNoOfPosts"
:[46,16,9,2,0,1,0,0],"timeCategories":["< 10 minutes","10-30 minutes","30-90 minutes","1.5-6 hours","6-12
hours","12-24 hours","24-72 hours","> 72 hours"],"avgResponseTime":"35.4 minutes"}


I am manipulating like this:

IRNameValueBean bean = null;
ArrayList<IRNameValueBean> rowObjects = new ArrayList<IRNameValueBean>();

for (int i = 0; i < json.getJSONArray("dailyResponseRate").length(); i++)
{
bean = new IRNameValueBean();
//Here Getting Execption
bean.setDate(json.getJSONArray("categories").getString(i));
bean.setDailyResponseRate(json.getJSONArray("dailyResponseRate").getString(i));

rowObjects.add(bean);
}

Answer Source

Your JsonArray looks to be made out of doubles

"dailyResponseRate":[33,.33,47.06,33.33,77. ...

Use this

bean.setDailyResponseRate(String.valueOf(json.getJSONArray("dailyResponseRate").getDouble(i)));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download