gpt4 book ai didi

android - JSON异常 : Not a primitive array?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:56:44 26 4
gpt4 key购买 nike

我在从 URL 接收 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 中完成的,所以没有问题。知道是什么原因造成的吗?

最佳答案

我遇到了同样的问题。这是因为这一行:

JSONArray leagueArray = new JSONArray(jsonArray);

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

new JSONArray(jsonArray)

只是做:

leagueArray = jsonArray;

附带说明一下,我认为您的意思是:

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

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

希望这有帮助:)

关于android - JSON异常 : Not a primitive array?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23146141/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com