org.json.JSONException: значение в org.json.JSON.typeMismatch
Я анализирую некоторые данные по ссылке, которая содержит список таблицы премьер-лиги. Мне нравится использовать его, чтобы показать в виде списка. Но я получаю ошибки 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"
}}
5 ответов
Я нашел решение.
Класс анализатора JSON:
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 KEY_ID = "position";
public static final String KEY_NAME = "team";
private JSONObject users = null;
private JSONArray user2=null;
private JSONObject user3=null;
private String json;
public ParseJSON(String json){
this.json = json;
}
protected void parseJSON() {
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(json);
users = jsonObject.getJSONObject(JSON_ARRAY);
try {
user2=users.getJSONArray(CHILD_ARRAY);
position1 = new String[user2.length()];
team = new String[user2.length()];
points=new String[user2.length()];
for (int i = 0; i < user2.length(); i++) {
JSONObject jo = user2.getJSONObject(i);
try {
user3=jo.getJSONObject("overall");
points[i] = user3.getString("points");
System.out.println("Message me: "+points[i]);
}catch (Exception e)
{
e.printStackTrace();
}
position1[i] = jo.getString(KEY_ID);
team[i] = jo.getString(KEY_NAME);
System.out.println("Message me: "+position1[i]);
System.out.println("Message me: "+team[i]);
}
}catch (Exception e)
{
e.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Вы всегда будете получать ошибку, потому что "данные" - это JSONObject, а не JSONArray
users = jsonObject.getJSONArray (JSON_ARRAY);
ваш JSON, где данные JSONObject
{
"data":{
"standings":[
{
"identifier":"5hichhljwlipyqvghhr0banvd2gehtcf",
"position":1,
"team_identifier":"akppwaoizlxbsa66oupfkawevutbnjxp",
"tea
Сразу после jsonObject = new JSONObject(json);
у вас есть объект JSON data
а не массив JSON. Так что попробуйте это
jsonObject = new JSONObject(json);
JSONObject standings = jsonObject.getJSONObject("data");
//Continue with the code
Вы берете JSONObject в JSONArray, что неправильно.
users = jsonObject.getJSONArray(JSON_ARRAY);
измените тип данных ваших пользователей с JSONArray на JSONObject и обновите приведенную выше строку следующим образом:
users = jsonObject.getJSONObject(JSON_ARRAY);
Я должен изменить код. пожалуйста следуйте за этим.
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();
}}}