我正在从包含高级联赛表列表的链接中解析一些数据。我喜欢用它来显示列表视图。但是我得到了JSONException和JSON.typeMismatch错误。
以下是我的代码:
public class ParseJSON {
public static String[] position1;
public static String[] team;
public static String[] points;
public static final String JSON_ARRAY = "data";
public static final String CHILD_ARRAY = "standings";
public static final String CHILDS_CHILD_ARRAY = "overall";
public static final String KEY_POSITION = "position";
public static final String KEY_TEAM = "team";
public static final String KEY_POINTS = "points";
private JSONArray users = null, user2=null;
private String json;
public ParseJSON(String json){
this.json = json;
}
protected void parseJSON(){
JSONObject jsonObject=null, jsonObject1=null;
try {
jsonObject = new JSONObject(json);
users = jsonObject.getJSONArray(JSON_ARRAY);
try {
jsonObject1=new JSONObject(json);
user2=jsonObject1.getJSONArray(CHILD_ARRAY);
position1 = new String[user2.length()];
team = new String[user2.length()];
for(int i=0;i<user2.length();i++){
JSONObject jo = user2.getJSONObject(i);
position1[i] = jo.getString(KEY_POSITION);
team[i] = jo.getString(KEY_TEAM);
/*points[i] = jo.getString(KEY_POINTS);*/
}
}catch (JSONException e) {
e.printStackTrace();
}
points = new String[users.length()];
} catch (JSONException e) {
e.printStackTrace();
}}}
我收到错误消息
> W/System.err: org.json.JSONException: Value {"standings":[{"identifier":"5hichhljwlipyqvghhr0banvd2gehtcf","position":1,"team_identifier":"akppwaoizlxbsa66oupfkawevutbnjxp","team":"Liverpool","overall":{"wins":8,"draws":2,"losts":1,"points":26,"scores":30,"..."","matches_p
W/System.err: at org.json.JSON.typeMismatch(JSON.java:100)
W/System.err: at org.json.JSONObject.getJSONArray(JSONObject.java:588)
W/System.err: at d33pzz.example.com.dataparsingexample.ParseJSON.parseJSON(ParseJSON.java:37)
W/System.err: at d33pzz.example.com.dataparsingexample.MainActivity.showJSON(MainActivity.java:61)
W/System.err: at d33pzz.example.com.dataparsingexample.MainActivity.access$000(MainActivity.java:17)
W/System.err: at d33pzz.example.com.dataparsingexample.MainActivity$1.onResponse(MainActivity.java:44)
W/System.err: at d33pzz.example.com.dataparsingexample.MainActivity$1.onResponse(MainActivity.java:41)
W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
W/System.err: at android.os.Handler.handleCallback(Handler.java:746)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:148)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5443)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
任何人都可以指导我做错的地方吗?这个JSON格式如何解析和显示:
{
"data":{
"standings":[
{
"identifier":"5hichhljwlipyqvghhr0banvd2gehtcf",
"position":1,
"team_identifier":"akppwaoizlxbsa66oupfkawevutbnjxp",
"team":"Liverpool",
"overall":{
"wins":8,
"draws":2,
"losts":1,
"points":26,
"scores":30,
"conceded":14,
"last_5":"",
"matches_played":11,
"goal_difference":16
},
"home":{
"wins":4,
"draws":1,
"losts":0,
"scores":17,
"conceded":4,
"points":13,
"last_5":"",
"matches_played":5,
"goal_difference":13
},
"away":{
"wins":4,
"draws":1,
"losts":1,
"scores":13,
"conceded":10,
"points":13,
"last_5":"",
"matches_played":6,
"goal_difference":3
},
"penalization_points":0
},
{
"identifier":"psnb5ewiti9ejktz9w7v0lhgq06eorih",
"position":2,
"team_identifier":"blfamr89lxeyywtsraiqzq5p5zuz57i6",
"team":"Chelsea",
"overall":{
"wins":8,
"draws":1,
"losts":2,
"points":25,
"scores":26,
"conceded":9,
"last_5":"",
"matches_played":11,
"goal_difference":17
},
"home":{
"wins":5,
"draws":0,
"losts":1,
"scores":18,
"conceded":3,
"points":15,
"last_5":"",
"matches_played":6,
"goal_difference":15
},
"away":{
"wins":3,
"draws":1,
"losts":1,
"scores":8,
"conceded":6,
"points":10,
"last_5":"",
"matches_played":5,
"goal_difference":2
},
"penalization_points":0
}
],
"statusCode":"200",
"errorCode":"0",
"statusReason":"OK"
}}
答案 0 :(得分:0)
您正在将JSONObject带入JSONArray,这是错误的。
users = jsonObject.getJSONArray(JSON_ARRAY);
将您的用户数据类型从JSONArray更改为JSONObject并更新以上行:
users = jsonObject.getJSONObject(JSON_ARRAY);
答案 1 :(得分:0)
在jsonObject = new JSONObject(json);
之后你有一个json对象data
而不是json数组。试试这个
jsonObject = new JSONObject(json);
JSONObject standings = jsonObject.getJSONObject("data");
//Continue with the code
答案 2 :(得分:0)
您将始终收到错误,因为“data”是JSONObject而不是JSONArray
users = jsonObject.getJSONArray(JSON_ARRAY);
你的json数据是JSONObject
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
/* GUARD: was there an error? */
guard (error == nil) else {
print("There was an error with your request: \(error)")
return
}
/* GUARD: Did we get a successful 2XX response? */
guard let statusCode = (response as? HTTPURLResponse)?.statusCode, statusCode >= 200 && statusCode <= 299 else {
print("Your request returned a status code other than 2XX!")
return
}
/* GUARD: was there any data returned? */
guard let data = data else {
print("No data was returned by the request!")
return
}
//Process Data Here.
print("Data: \(data)")
}
task.resume()
答案 3 :(得分:0)
我已经更改了代码。请关注它。
public class ParseJSON {
public static String[] position1;
public static String[] team;
public static String[] points;
public static final String JSON_OBJECT_DATA = "data";
public static final String CHILD_ARRAY = "standings";
public static final String CHILDS_CHILD_ARRAY = "overall";
public static final String KEY_ID = "position";
public static final String KEY_NAME = "team";
public static final String KEY_EMAIL = "points";
private JSONObject users = null;
private JSONArray user2=null;
private String json;
public ParseJSON(String json){
this.json = json;
}
protected void parseJSON(){
JSONObject jsonObject=null, jsonObject1=null;
try {
jsonObject = new JSONObject(json);
users = jsonObject.getJSONObject(JSON_OBJECT_DATA);
try {
user2=users.getJSONArray(CHILD_ARRAY);
position1 = new String[user2.length()];
team = new String[user2.length()];
for(int i=0;i<user2.length();i++){
JSONObject jo = user2.getJSONObject(i);
position1[i] = jo.getString(KEY_ID);
team[i] = jo.getString(KEY_NAME);
/*points[i] = jo.getString(KEY_EMAIL);*/
}
}catch (JSONException e) {
e.printStackTrace();
}
points = new String[users.length()];
} catch (JSONException e) {
e.printStackTrace();
}}}
答案 4 :(得分:0)
我找到了解决方案。
JSON解析器类:
switch upgradeNumber {
case 0:
production = 1;
case 1:
production = 2;
case 2:
production = 3;
default:
production = 1;
}