JSONException:不是原始数组?

时间:2014-04-18 01:42:22

标签: android arrays json

我在从网址接收JSON数组时遇到问题。我已经验证我的链接正常,并且返回了正确的JSON数组,它甚至显示在错误消息中。我不确定这意味着什么。

错误:

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)

代码:

    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
}]

这一切都是在AsyncTask中完成的,所以没有问题。知道是什么导致了这个吗?

4 个答案:

答案 0 :(得分:9)

我有同样的问题。正是因为这条线:

JSONArray leagueArray = new JSONArray(jsonArray);

你基本上是在创建两个不同的JSONArrays,第二个只是指向第一个,但你不能以这种方式实例化第二个数组,否则它会抛出错误。因此,您不需要这样做:

new JSONArray(jsonArray)

只是这样做:

leagueArray = jsonArray;

另一方面,我认为你的意思是:

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

你的第一个结果是一个数组(用'[]'表示),但内部子项是一个对象(用'{}'表示)

希望这会有所帮助:)

答案 1 :(得分:3)

看起来你正试图在其中包含一些对象的JSONArray。尝试做这样的事情:

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.

}

答案 2 :(得分:1)

第二个JSONArray指向第一个,所以就这样做:

JSONArray firstArray = (JSONArray) secondArray;

ofc,仅在您需要时施放!

答案 3 :(得分:1)

JSON ArrayResponse
[
{
    "name" : "Ravi Tamada", 
    "email" : "ravi8x@gmail.com",
    "phone" : {
        "home" : "08947 000000",
        "mobile" : "9999999999"
    }
    },
    {
    "name" : "Tommy", 
    "email" : "tommy@gmail.com",
    "phone" : {
        "home" : "08946 000000",
        "mobile" : "0000000000"
    }
    }
]

在JsonArray(响应)中获得Volley库的响应之后,不要通过创建新的JSonArray来进行原始响应。

使用以下方法:

JSONObject jsonObject = response.getJSONObject(i);

String name = jsonObject.getString("name");
String email = jsonObject.getString("email");

JSONObject jsonObject2 = jsonObject.getJSONObject("phone");
String home = jsonObject2.getString("home");
String mobile = jsonObject2.getString("mobile");