Search code examples
javaandroidjsonandroid-volleyjsonexception

org.json.JSONException: Value at org.json.JSON.typeMismatch


I am parsing some data from a link which contains the list of premier league table. I like to use it to show in a list view. But I get JSONException and JSON.typeMismatch errors.

Below is my code:

 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();
        }}}

I'm getting error message

>  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)

Can anyone please guide me where I'm doing wrong? This JSON format how to parse and display:

{
"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"
}}

Solution

  • I've found the solution.

    JSON parser class:

    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();
                }
            }
        }