简体   繁体   中英

Parsing json in Android - org.json.JSONException typeMismatch

I understand this question has been asked numerous times and I've looked at a lot of examples and questions with answers on this. Yet, even looking at what I think should be right, I am still getting the error from logcat of org.json.JSONException: Value of at org.json.JSON.typeMismatch(JSON.java:100). There is some confusion on my part of how to go about extracting the info from the json or something in the json causing it to happen. Both of which I'm struggling to locate.

To start here is the full json

{
"data": "{\"haz\":[{\"id\":220121,\"rwn\":\"US-90 EAST\",\"dir\":0,\"cs\":\"MCCART ST\",\"lat\":29.777589,\"lon\":-95.284037,\"acc\":\"Minor Accident/Collision\",\"vi\":3,\"la\":\"Alternate Lanes\",\"mlb\":2,\"flb\":2,\"rlb\":0,\"hlb\":0,\"slb\":1,\"omb\":0,\"osb\":0,\"dc\":\"5/28/2014 4:34:41 AM\",\"dm\":\"12/31/9999 11:59:59 PM\",\"sum\":\"Right Shoulder, Right Lane, Center Lane\",\"toa\":0,\"toi\":0}],\"accident\":[{\"id\":220116,\"rwn\":\"IH-10 KATY\",\"dir\":0,\"cs\":\"WESTGREEN BLVD\",\"lat\":29.78518,\"lon\":-95.73529,\"acc\":\"Major Accident/Collision\",\"vi\":1,\"la\":\"Alternate Lanes\",\"mlb\":2,\"flb\":0,\"rlb\":0,\"hlb\":0,\"slb\":0,\"omb\":0,\"osb\":0,\"dc\":\"5/28/2014 3:40:24 AM\",\"dm\":\"12/31/9999 11:59:59 PM\",\"sum\":\"Left Lane, Center Lane\",\"toa\":2,\"toi\":1},{\"id\":220130,\"rwn\":\"IH-45 GULF\",\"dir\":4,\"cs\":\"SCARSDALE BLVD\",\"lat\":29.599,\"lon\":-95.1976,\"acc\":\"Minor Accident/Collision\",\"vi\":2,\"la\":\"Alternate Lanes\",\"mlb\":1,\"flb\":0,\"rlb\":0,\"hlb\":0,\"slb\":0,\"omb\":0,\"osb\":0,\"dc\":\"5/28/2014 6:11:32 AM\",\"dm\":\"12/31/9999 11:59:59 PM\",\"sum\":\"Center Lane\",\"toa\":1,\"toi\":1},{\"id\":220129,\"rwn\":\"IH-45 GULF\",\"dir\":4,\"cs\":\"SCARSDALE BLVD\",\"lat\":29.599,\"lon\":-95.1976,\"acc\":\"Minor Accident/Collision\",\"vi\":2,\"la\":\"Alternate Lanes\",\"mlb\":0,\"flb\":0,\"rlb\":0,\"hlb\":0,\"slb\":0,\"omb\":0,\"osb\":0,\"dc\":\"5/28/2014 6:06:19 AM\",\"dm\":\"12/31/9999 11:59:59 PM\",\"sum\":\"Unknown\",\"toa\":1,\"toi\":1},{\"id\":220127,\"rwn\":\"IH-45 NORTH\",\"dir\":4,\"cs\":\"N MAIN ST\",\"lat\":29.79018,\"lon\":-95.37202,\"acc\":\"Minor Accident/Collision\",\"vi\":2,\"la\":\"Alternate Lanes\",\"mlb\":1,\"flb\":0,\"rlb\":0,\"hlb\":0,\"slb\":0,\"omb\":0,\"osb\":0,\"dc\":\"5/28/2014 5:45:21 AM\",\"dm\":\"12/31/9999 11:59:59 PM\",\"sum\":\"Left Lane\",\"toa\":1,\"toi\":1},{\"id\":220131,\"rwn\":\"IH-610 SOUTH LOOP\",\"dir\":0,\"cs\":\"SH-225\",\"lat\":29.7096,\"lon\":-95.2674,\"acc\":\"Major Accident/Collision\",\"vi\":2,\"la\":\"Alternate Lanes\",\"mlb\":2,\"flb\":0,\"rlb\":0,\"hlb\":0,\"slb\":0,\"omb\":0,\"osb\":0,\"dc\":\"5/28/2014 6:18:55 AM\",\"dm\":\"12/31/9999 11:59:59 PM\",\"sum\":\"Right Lane, Center Lane\",\"toa\":1,\"toi\":1},{\"id\":220126,\"rwn\":\"SOUTH SAM HOUSTON TOLLWAY\",\"dir\":0,\"cs\":\"FUQUA\",\"lat\":29.60503,\"lon\":-95.47413,\"acc\":\"Major Accident/Collision\",\"vi\":2,\"la\":\"Alternate Lanes\",\"mlb\":1,\"flb\":0,\"rlb\":0,\"hlb\":0,\"slb\":1,\"omb\":0,\"osb\":0,\"dc\":\"5/28/2014 5:44:54 AM\",\"dm\":\"12/31/9999 11:59:59 PM\",\"sum\":\"Left Shoulder, Left Lane\",\"toa\":1,\"toi\":1}],\"highwater\":[{\"id\":220123,\"rwn\":\"IH-610 NORTH LOOP\",\"dir\":3,\"cs\":\"IH-45 NORTH\",\"lat\":29.8132,\"lon\":-95.3752,\"acc\":\"Minor Accident/Collision\",\"vi\":0,\"la\":\"Alternate Lanes\",\"mlb\":0,\"flb\":0,\"rlb\":1,\"hlb\":0,\"slb\":0,\"omb\":0,\"osb\":0,\"dc\":\"5/28/2014 4:53:25 AM\",\"dm\":\"12/31/9999 11:59:59 PM\",\"sum\":\"Exit Ramp\",\"toa\":0,\"toi\":2},{\"id\":220118,\"rwn\":\"US-290 NORTHWEST\",\"dir\":0,\"cs\":\"FM-529\",\"lat\":29.8795,\"lon\":-95.569,\"acc\":\"Minor Accident/Collision\",\"vi\":0,\"la\":\"Alternate Lanes\",\"mlb\":0,\"flb\":1,\"rlb\":0,\"hlb\":0,\"slb\":0,\"omb\":0,\"osb\":0,\"dc\":\"5/28/2014 4:12:17 AM\",\"dm\":\"12/31/9999 11:59:59 PM\",\"sum\":\"1 Frontage Road Lane\",\"toa\":0,\"toi\":2},{\"id\":220124,\"rwn\":\"US-90 ALTERNATE\",\"dir\":0,\"cs\":\"Lane Dr\",\"lat\":29.573644,\"lon\":-95.773905,\"acc\":\"Minor Accident/Collision\",\"vi\":0,\"la\":\"Alternate Lanes\",\"mlb\":0,\"flb\":0,\"rlb\":0,\"hlb\":0,\"slb\":0,\"omb\":0,\"osb\":0,\"dc\":\"5/28/2014 5:03:40 AM\",\"dm\":\"12/31/9999 11:59:59 PM\",\"sum\":\"Unknown\",\"toa\":0,\"toi\":2},{\"id\":220125,\"rwn\":\"US-90 ALTERNATE\",\"dir\":0,\"cs\":\"Chimney Rock Rd\",\"lat\":29.640602,\"lon\":-95.482381,\"acc\":\"Minor Accident/Collision\",\"vi\":0,\"la\":\"Alternate Lanes\",\"mlb\":0,\"flb\":0,\"rlb\":0,\"hlb\":0,\"slb\":0,\"omb\":0,\"osb\":0,\"dc\":\"5/28/2014 5:06:48 AM\",\"dm\":\"12/31/9999 11:59:59 PM\",\"sum\":\"Unknown\",\"toa\":0,\"toi\":2}],\"ice\":null,\"lostload\":[{\"id\":220122,\"rwn\":\"IH-10 EAST\",\"dir\":3,\"cs\":\"FREEPORT BLVD\",\"lat\":29.7707,\"lon\":-95.1778,\"acc\":\"Minor Accident/Collision\",\"vi\":0,\"la\":\"Alternate Lanes\",\"mlb\":0,\"flb\":2,\"rlb\":0,\"hlb\":0,\"slb\":0,\"omb\":0,\"osb\":0,\"dc\":\"5/28/2014 4:34:54 AM\",\"dm\":\"12/31/9999 11:59:59 PM\",\"sum\":\"2 Frontage Road Lanes\",\"toa\":0,\"toi\":4}],\"roaddebris\":[{\"id\":220117,\"rwn\":\"US-290 NORTHWEST\",\"dir\":3,\"cs\":\"FM-529\",\"lat\":29.8369039,\"lon\":-95.4991506,\"acc\":\"Minor Accident/Collision\",\"vi\":0,\"la\":\"Alternate Lanes\",\"mlb\":0,\"flb\":2,\"rlb\":0,\"hlb\":0,\"slb\":0,\"omb\":0,\"osb\":0,\"dc\":\"5/28/2014 4:11:35 AM\",\"dm\":\"12/31/9999 11:59:59 PM\",\"sum\":\"2 Frontage Road Lanes\",\"toa\":0,\"toi\":5}],\"stall\":[{\"id\":220128,\"rwn\":\"IH-45 NORTH\",\"dir\":4,\"cs\":\"N MAIN ST\",\"lat\":29.79018,\"lon\":-95.37202,\"acc\":\"Major Accident/Collision\",\"vi\":1,\"la\":\"Center Lane\",\"mlb\":1,\"flb\":0,\"rlb\":0,\"hlb\":0,\"slb\":0,\"omb\":0,\"osb\":0,\"dc\":\"5/28/2014 6:03:41 AM\",\"dm\":\"12/31/9999 11:59:59 PM\",\"sum\":\"Left Lane, Center Lane\",\"toa\":0,\"toi\":6},{\"id\":220129,\"rwn\":\"IH-45 GULF\",\"dir\":4,\"cs\":\"SCARSDALE BLVD\",\"lat\":29.599,\"lon\":-95.3586,\"acc\":\"Minor Accident/Collision\",\"vi\":2,\"la\":\"Alternate Lanes\",\"mlb\":0,\"flb\":0,\"rlb\":0,\"hlb\":0,\"slb\":0,\"omb\":0,\"osb\":0,\"dc\":\"5/28/2014 6:06:19 AM\",\"dm\":\"12/31/9999 11:59:59 PM\",\"sum\":\"Unknown\",\"toa\":0,\"toi\":6}],\"fire\":[{\"id\":220119,\"rwn\":\"BELTWAY 8-NORTH\",\"dir\":0,\"cs\":\"LEE RD\",\"lat\":29.9396,\"lon\":-95.3034,\"acc\":\"Minor Accident/Collision\",\"vi\":0,\"la\":\"Alternate Lanes\",\"mlb\":0,\"flb\":2,\"rlb\":0,\"hlb\":0,\"slb\":0,\"omb\":0,\"osb\":0,\"dc\":\"5/28/2014 4:22:56 AM\",\"dm\":\"12/31/9999 11:59:59 PM\",\"sum\":\"2 Frontage Road Lanes\",\"toa\":0,\"toi\":7}]}",
"error": 0,
"when": "6/6/2016 12:33:59 PM"}

For the parsing I attempt to do it like

JSONObject reader = new JSONObject(results);
JSONObject data = reader.getJSONObject("data");

I believe "data" is a JSONObject of reader and itself is filled with numerous JSONArray. This is where the error is thrown when attempting to read what I believe to be the JSONobject named "data"

Error (only snippet shown as its spitting out the entire json string)

org.json.JSONException: Value {"haz":[{"id":220121,"rwn":"US-90 EAST","dir":0,"cs":"MCCART ST","
at org.json.JSON.typeMismatch(JSON.java:100)

Anyone see what I'm doing incorrect here?

Here is the full code I have for getting the info up to the error from the call back function

IncidentsList incidentsList = new IncidentsList();
JSONObject reader = new JSONObject(results);
        int error = reader.getInt("error");
        //there should always be a value for errors
        incidentsList.ERRORS = error == 1;

        //no errors, continue
        if(error == 0){
            Log.i("jsonreader", "no errors, read data object");
            JSONObject data = reader.getJSONObject("data");
            Log.i("jsonreader", "read data object");
        }

It never gets past the JSONObject data = reader.getJSONObject("data"); The logcat never shows the 2nd info statement

Thanks

Your json string is invalid. It should end like

            "toi": 2
        }]
    },
    "errors": 0
}

but its ending like

            "toi": 2
        }], <------- this comma shouldn't be there
    },
    "errors": 0
}

Update

Error is "data": "{\\"haz\\": . It should be "data": {\\"haz\\":

and should end like

        },
    "error": 0,
    "when": "6/6/2016 12:33:59 PM"
} 

I believe "data" is a JSONObject of reader

Nope, it's actually a string. Notice the quote before the brace.

"data": "{

If you have control over the JSON, I would encourage you to make it an object so it's both easier to read and parse.

JSON format having extra comma "," as @Rohit5k2 said, I suggest you to use GSON to easily parse your JSON, instead of parse each element by jsonObject and jsonArray.

your GSON model class can be

import com.google.gson.Gson;
import java.util.List;

public class Resp {

    public DataEntity data;
    public int errors;

    public static Resp objectFromData(String str) {

        return new Gson().fromJson(str, Resp.class);
    }

    public static class DataEntity {
        public List<HazEntity> haz;
        public List<AccidentEntity> accident;
        public List<HighwaterEntity> highwater;

        public static DataEntity objectFromData(String str) {

            return new Gson().fromJson(str, DataEntity.class);
        }

        public static class HazEntity {
            public int id;
            public String rwn;
            public int dir;
            public String cs;
            public double lat;
            public double lon;
            public String acc;
            public int vi;
            public String la;
            public int mlb;
            public int flb;
            public int rlb;
            public int hlb;
            public int slb;
            public int omb;
            public int osb;
            public String dc;
            public String dm;
            public String sum;
            public int toa;
            public int toi;

            public static HazEntity objectFromData(String str) {

                return new Gson().fromJson(str, HazEntity.class);
            }
        }

        public static class AccidentEntity {
            public int id;
            public String rwn;
            public int dir;
            public String cs;
            public double lat;
            public double lon;
            public String acc;
            public int vi;
            public String la;
            public int mlb;
            public int flb;
            public int rlb;
            public int hlb;
            public int slb;
            public int omb;
            public int osb;
            public String dc;
            public String dm;
            public String sum;
            public int toa;
            public int toi;

            public static AccidentEntity objectFromData(String str) {

                return new Gson().fromJson(str, AccidentEntity.class);
            }
        }

        public static class HighwaterEntity {
            public int id;
            public String rwn;
            public int dir;
            public String cs;
            public double lat;
            public double lon;
            public String acc;
            public int vi;
            public String la;
            public int mlb;
            public int flb;
            public int rlb;
            public int hlb;
            public int slb;
            public int omb;
            public int osb;
            public String dc;
            public String dm;
            public String sum;
            public int toa;
            public int toi;

            public static HighwaterEntity objectFromData(String str) {

                return new Gson().fromJson(str, HighwaterEntity.class);
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM