如何在Android上解析Java中格式错误的Json?

时间:2016-03-12 15:46:20

标签: java android json

所以我有一个Jsonobject作为我服务器的响应,格式如下:

{"SubjectName":["Irish"],"BookName":["Bun Go Barr 1"]}{"WorksheetID":[5],"WorksheetName":["TESTS"]}

通常为了获得结果,我只需按照以下方式在对象内的特定数组中进行基本循环:

try {
            jsonArray = jsonObject.getJSONArray("SubjectName");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        BookSubject = new String[jsonArray.length()];
        for (int i = 0; i < jsonArray.length(); i++) {
            try {
                BookSubject[i] = jsonArray.getString(i);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

然而,似乎在这种情况下有一个额外的“{”分隔一些数组意味着当我尝试为工作表ID执行此操作时,我在logcat中得到以下内容:

 03-12 08:21:02.019  10836-10836/net.azurewebsites.cosy W/System.err﹕ org.json.JSONException: No value for WorksheetID
    03-12 08:21:02.019  10836-10836/net.azurewebsites.cosy W/System.err﹕ at org.json.JSONObject.get(JSONObject.java:355)
    03-12 08:21:02.019  10836-10836/net.azurewebsites.cosy W/System.err﹕ at org.json.JSONObject.getJSONArray(JSONObject.java:549)
    03-12 08:21:02.019  10836-10836/net.azurewebsites.cosy W/System.err﹕ at net.azurewebsites.cosy.Classroom.onCreate(Classroom.java:114)
    03-12 08:21:02.019  10836-10836/net.azurewebsites.cosy W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5231)
    03-12 08:21:02.019  10836-10836/net.azurewebsites.cosy W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    03-12 08:21:02.019  10836-10836/net.azurewebsites.cosy W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
    03-12 08:21:02.019  10836-10836/net.azurewebsites.cosy W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
    03-12 08:21:02.019  10836-10836/net.azurewebsites.cosy W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:135)
    03-12 08:21:02.019  10836-10836/net.azurewebsites.cosy W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    03-12 08:21:02.019  10836-10836/net.azurewebsites.cosy W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
    03-12 08:21:02.019  10836-10836/net.azurewebsites.cosy W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
    03-12 08:21:02.019  10836-10836/net.azurewebsites.cosy W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5001)
    03-12 08:21:02.023  10836-10836/net.azurewebsites.cosy W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
    03-12 08:21:02.023  10836-10836/net.azurewebsites.cosy W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
    03-12 08:21:02.023  10836-10836/net.azurewebsites.cosy W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    03-12 08:21:02.027  10836-10836/net.azurewebsites.cosy W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    03-12 08:21:02.027  10836-10836/net.azurewebsites.cosy W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
    03-12 08:21:02.031  10836-10836/net.azurewebsites.cosy D/AndroidRuntime﹕ Shutting down VM

任何人都知道我怎么能把它拿起来?

1 个答案:

答案 0 :(得分:2)

您的json无效。

您可以测试json在线工具http://www.jsoneditoronline.org/

更改表格:

{"SubjectName":["Irish"],"BookName":["Bun Go Barr 1"]}{"WorksheetID":[5],"WorksheetName":["TESTS"]}

致:

[
  {
    "SubjectName": "Irish",
    "BookName": "Bun Go Barr 1"
  },
  {
    "WorksheetID": 5,
    "WorksheetName": "TESTS"
  }
]