Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONException: Not a primitive array?

I'm having problems with receiving a JSON array from a URL. I have verified that my link is ok, and the correct JSON array is returned, and it even shows in the error message. I am unsure what it means.

Error:

04-17 21:34:04.435  21842-22217/edu.appdesign.leaguestats W/System.err﹕ org.json.JSONException: Not a primitive array: class org.json.JSONArray
04-17 21:34:04.435  21842-22217/edu.appdesign.leaguestats W/System.err﹕ at org.json.JSONArray.<init>(JSONArray.java)
04-17 21:34:04.445  21842-22217/edu.appdesign.leaguestats W/System.err﹕ at edu.appdesign.leaguestats.GetStaticData$GetSummary.doInBackground(GetStaticData.java:90)
04-17 21:34:04.445  21842-22217/edu.appdesign.leaguestats W/System.err﹕ at edu.appdesign.leaguestats.GetStaticData$GetSummary.doInBackground(GetStaticData.java:76)
04-17 21:34:04.445  21842-22217/edu.appdesign.leaguestats W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java)
04-17 21:34:04.445  21842-22217/edu.appdesign.leaguestats W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java)
04-17 21:34:04.445  21842-22217/edu.appdesign.leaguestats W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java)
04-17 21:34:04.445  21842-22217/edu.appdesign.leaguestats W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java)
04-17 21:34:04.445  21842-22217/edu.appdesign.leaguestats W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java)
04-17 21:34:04.445  21842-22217/edu.appdesign.leaguestats W/System.err﹕ at java.lang.Thread.run(Thread.java)

Code:

    JSONArray jsonArray = jsonArrayParser.getJSONFromUrl(url2);
    JSONArray leagueArray = new JSONArray(jsonArray);
    summary.rTier = leagueData.getString("tier");
    summary.rLeague = leagueData.getString("leagueName");
    summary.rRank = leagueData.getString("rank");
    summary.rLeaguePoints = leagueData.getString("leaguePoints");

JSON:

[{
   "isHotStreak": false,
   "isFreshBlood": false,
   "leagueName": "Udyr's Lancers",
   "isVeteran": false,
   "tier": "GOLD",
   "lastPlayed": -1,
   "playerOrTeamId": "23591778",
   "leaguePoints": 0,
   "rank": "V",
   "isInactive": false,
   "queueType": "RANKED_SOLO_5x5",
   "playerOrTeamName": "NV43",
   "wins": 83
}]

This is all being done from within an AsyncTask, so there is no issue there. Any idea what could be causing this?

like image 623
Nate Avatar asked Apr 18 '14 01:04

Nate


3 Answers

I had the same issue. It is because of this line:

JSONArray leagueArray = new JSONArray(jsonArray);

You are basically creating 2 different JSONArrays, the second one is just pointing back to first, but you can't instantiate the second array this way, or it will throw the error. Thus you don't need to do:

new JSONArray(jsonArray)

just do:

leagueArray = jsonArray;

On a side note, I think what you meant to do was:

JSONArray jsonArray = jsonArrayParser.getJSONFromUrl(url2);
JSONObject leagueObject = jsonArray.getJSONObject(0);

Your first result is an Array (denoted by '[]') but the inner child is an object (denoted by '{}')

Hope this helps :)

like image 134
Machine Tribe Avatar answered Nov 20 '22 11:11

Machine Tribe


It looks like you are trying to pull down a JSONArray with some objects in it. Try doing something like this:

JSONArray jsonArray = jsonArrayParser.getJSONFromUrl(url2);
for (int i=0; i<jsonArray.length(); i++) {
    JSONObject leagueData = jsonArray.getJSONObject(i);
    String tier = leagueData.getString("tier");
    String leagueName = leagueData.getString("leagueName");
    String rank = leagueData.getString("rank");
    String leaguePoints = leagueData.getString("leaguePoints");

    // Whatever you want to do with these fields.

}
like image 42
Dan Harms Avatar answered Nov 20 '22 10:11

Dan Harms


the second JSONArray is pointing back to first, so just do this:

JSONArray firstArray = (JSONArray) secondArray;

ofc, cast only if you need!

like image 3
Francesco O'Bioh D'Agostino Avatar answered Nov 20 '22 12:11

Francesco O'Bioh D'Agostino