JSONException但有效的JSON字符串

时间:2014-12-18 23:27:05

标签: android json exception android-studio

[
    {
        "ID": 1,
        "EventDate": "2014-10-10T00:00:00",
        "EventName": "Geburtstagsparty Michael",
        "EventDescription": "19.Geburtstag",
        "ExchangeMoney": 345.5,
        "Rent": null,
        "CleanMoney": null,
        "Earmarked": false,
        "ResponsibleMember": {
            "ID": "d3fbc332-99ab-49c8-8374-4ef192db78e4",
            "Firstname": null,
            "Lastname": "Aigner",
            "Birthday": "0001-01-01T00:00:00",
            "Telephonenumber": null,
            "Street": null,
            "PostalCode": null,
            "City": null,
            "Username": null,
            "Password": null,
            "eMail": null
        },
        "EventType": "Geburtstagsparty"
    },
    {
        "ID": 2,
        "EventDate": "2014-12-31T00:00:00",
        "EventName": "Silvesterparty",
        "EventDescription": "Happy new Year!",
        "ExchangeMoney": 551.2,
        "Rent": null,
        "CleanMoney": null,
        "Earmarked": false,
        "ResponsibleMember": {
            "ID": "d3fbc332-99ab-49c8-8374-4ef192db78e4",
            "Firstname": null,
            "Lastname": "Aigner",
            "Birthday": "0001-01-01T00:00:00",
            "Telephonenumber": null,
            "Street": null,
            "PostalCode": null,
            "City": null,
            "Username": null,
            "Password": null,
            "eMail": null
        },
        "EventType": "Feiertags-Feier"
    }
]

我的Logcat说:

12-18 23:58:02.132  14399-14399/aignerkatzlberger.htlgrieskirchen.at.jugendraumapp W/System.err﹕           org.json.JSONException: Value [{"ID":1,"EventDate":"2014-10-10T00:00:00","EventName":"Geburtstagsparty Michael","EventDescription":"19.Geburtstag","ExchangeMoney":345.5000,"Rent":null,"CleanMoney":null,"Earmarked":false,"ResponsibleMember":{"ID":"d3fbc332-99ab-49c8-8374-4ef192db78e4","Firstname":null,"Lastname":"Aigner","Birthday":"0001-01-01T00:00:00","Telephonenumber":null,"Street":null,"PostalCode":null,"City":null,"Username":null,"Password":null,"eMail":null},"EventType":"Geburtstagsparty"},{"ID":2,"EventDate":"2014-12-31T00:00:00","EventName":"Silvesterparty","EventDescription":"Happy new Year!","ExchangeMoney":551.2000,"Rent":null,"CleanMoney":null,"Earmarked":false,"ResponsibleMember":{"ID":"d3fbc332-99ab-49c8-8374-4ef192db78e4","Firstname":null,"Lastname":"Aigner","Birthday":"0001-01-01T00:00:00","Telephonenumber":null,"Street":null,"PostalCode":null,"City":null,"Username":null,"Password":null,"eMail":null},"EventType":"Feiertags-Feier"}] of type java.lang.String cannot be converted to JSONArray
    12-18 23:58:02.132  14399-14399/aignerkatzlberger.htlgrieskirchen.at.jugendraumapp W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:111)
    12-18 23:58:02.132  14399-14399/aignerkatzlberger.htlgrieskirchen.at.jugendraumapp W/System.err﹕ at org.json.JSONArray.<init>(JSONArray.java:96)
    12-18 23:58:02.132  14399-14399/aignerkatzlberger.htlgrieskirchen.at.jugendraumapp W/System.err﹕ at org.json.JSONArray.<init>(JSONArray.java:108)
    12-18 23:58:02.132  14399-14399/aignerkatzlberger.htlgrieskirchen.at.jugendraumapp W/System.err﹕ at aignerkatzlberger.htlgrieskirchen.at.jugendraumapp.database.DataRepository.insertEvents(DataRepository.java:58)
    12-18 23:58:02.132  14399-14399/aignerkatzlberger.htlgrieskirchen.at.jugendraumapp W/System.err﹕ at aignerkatzlberger.htlgrieskirchen.at.jugendraumapp.database.DataRepository.access$000(DataRepository.java:32)
    12-18 23:58:02.132  14399-14399/aignerkatzlberger.htlgrieskirchen.at.jugendraumapp W/System.err﹕ at aignerkatzlberger.htlgrieskirchen.at.jugendraumapp.database.DataRepository$RequestTask.onPostExecute(DataRepository.java:134)
    12-18 23:58:02.132  14399-14399/aignerkatzlberger.htlgrieskirchen.at.jugendraumapp W/System.err﹕ at aignerkatzlberger.htlgrieskirchen.at.jugendraumapp.database.DataRepository$RequestTask.onPostExecute(DataRepository.java:91)
    12-18 23:58:02.132  14399-14399/aignerkatzlberger.htlgrieskirchen.at.jugendraumapp W/System.err﹕ at android.os.AsyncTask.finish(AsyncTask.java:632)
    12-18 23:58:02.142  14399-14399/aignerkatzlberger.htlgrieskirchen.at.jugendraumapp W/System.err﹕ at android.os.AsyncTask.access$600(AsyncTask.java:177)
    12-18 23:58:02.142  14399-14399/aignerkatzlberger.htlgrieskirchen.at.jugendraumapp W/System.err﹕ at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
    12-18 23:58:02.142  14399-14399/aignerkatzlberger.htlgrieskirchen.at.jugendraumapp W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
    12-18 23:58:02.142  14399-14399/aignerkatzlberger.htlgrieskirchen.at.jugendraumapp W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
    12-18 23:58:02.142  14399-14399/aignerkatzlberger.htlgrieskirchen.at.jugendraumapp W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5146)
    12-18 23:58:02.142  14399-14399/aignerkatzlberger.htlgrieskirchen.at.jugendraumapp W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
    12-18 23:58:02.142  14399-14399/aignerkatzlberger.htlgrieskirchen.at.jugendraumapp W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
    12-18 23:58:02.142  14399-14399/aignerkatzlberger.htlgrieskirchen.at.jugendraumapp W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
    12-18 23:58:02.142  14399-14399/aignerkatzlberger.htlgrieskirchen.at.jugendraumapp W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
    12-18 23:58:02.142  14399-14399/aignerkatzlberger.htlgrieskirchen.at.jugendraumapp W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

正如您在图片中看到的那样,如果我想将JSON字符串转换为JSONException,我会得到JSONArray。我不明白,因为有几个在线测试说我的JSON是有效的。

此外,我必须使用JSONArray作为根元素,因为有&#34; [&#34;根级括号。

那么我做错了什么?

private void insertEvents(String result) 
{
    if(result.equals("-1"))
    {
        handleError();
    }
    else
    {
        try 
        {
            // make JSON object
            JSONArray jArr = new JSONArray(result);
        }
        catch (Exception e)
        {
            // TODO: handle
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我能够使用以下代码重现您的问题:

    String s = "'[{ \"ID\": 1, \"EventDate\": \"2014-10-10T00:00:00\", \"EventName\": \"Geburtstagsparty Michael\", \"EventDescription\": \"19.Geburtstag\", \"ExchangeMoney\": 345.5, \"Rent\": null, \"CleanMoney\": null, \"Earmarked\": false, \"ResponsibleMember\": { \"ID\": \"d3fbc332-99ab-49c8-8374-4ef192db78e4\", \"Firstname\": null, \"Lastname\": \"Aigner\", \"Birthday\": \"0001-01-01T00:00:00\", \"Telephonenumber\": null, \"Street\": null, \"PostalCode\": null, \"City\": null, \"Username\": null, \"Password\": null, \"eMail\": null }, \"EventType\": \"Geburtstagsparty\" }, { \"ID\": 2, \"EventDate\": \"2014-12-31T00:00:00\", \"EventName\": \"Silvesterparty\", \"EventDescription\": \"Happy new Year!\", \"ExchangeMoney\": 551.2, \"Rent\": null, \"CleanMoney\": null, \"Earmarked\": false, \"ResponsibleMember\": { \"ID\": \"d3fbc332-99ab-49c8-8374-4ef192db78e4\", \"Firstname\": null, \"Lastname\": \"Aigner\", \"Birthday\": \"0001-01-01T00:00:00\", \"Telephonenumber\": null, \"Street\": null, \"PostalCode\": null, \"City\": null, \"Username\": null, \"Password\": null, \"eMail\": null }, \"EventType\": \"Feiertags-Feier\" } ]'";
    try {
        JSONArray arr = new JSONArray(s);
        Log.d(TAG, arr.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }

请注意,我将整个JSON字符串括在单引号中,因此我非常怀疑您的result变量也是如此。删除包含JSON的单引号解决了这个问题。

此外:请注意,您获得的错误是...of type java.lang.String cannot be converted to JSONArray。这意味着它将result视为一个字符串,如果result确实是一个JSON则不应该这样。这几乎只能意味着result是引号括起来的有效JSON,从而使JSON解析器认为它是一个字符串。