解析Android中的json - org.json.JSONException typeMismatch

时间:2016-06-06 17:20:14

标签: android json

我理解这个问题已经被多次询问了,我已经看了很多例子和问题以及答案。然而,即使看看我认为应该是正确的,我仍然从org.json.JSONException的logcat获取错误:org.json.JSON.typeMismatch(JSON.java:100)的值。对于如何从json中提取信息或json中的某些内容导致它发生,我有一些困惑。我正在努力找到这两个。

从这里开始是完整的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"}

对于解析,我试图像

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

我相信“数据”是读者的JSONObject,本身充满了无数的JSONArray。这是在尝试阅读我认为是名为“数据”的JSON对象时引发错误的地方

错误(仅显示整个json字符串吐出的片段)

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

有人在这里看到我做错了吗?

以下是我从回调函数

获取错误信息的完整代码
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");
        }

它永远不会超过JSONObject data = reader.getJSONObject(“data”); logcat从不显示第二个info语句

由于

3 个答案:

答案 0 :(得分:0)

您的json字符串无效。它应该像

那样结束
            "toi": 2
        }]
    },
    "errors": 0
}

但其结尾如

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

<强>更新

错误是"data": "{\"haz\":。它应该是"data": {\"haz\":

并且应该像

一样结束
        },
    "error": 0,
    "when": "6/6/2016 12:33:59 PM"
} 

答案 1 :(得分:0)

  

我相信&#34;数据&#34;是读者的JSONObject

不,它实际上是一个字符串。请注意括号前的引用。

"data": "{

如果您可以控制JSON,我建议您将其作为一个对象,以便更容易阅读和解析。

答案 2 :(得分:0)

JSON格式有额外的逗号“,”正如@ Rohit5k2所说, 我建议你使用GSON轻松解析你的JSON, 而不是通过jsonObject和jsonArray解析每个元素。

您的GSON模型类可以

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