gpt4 book ai didi

java - 如何在 Android 上用 Java 解析格式错误的 Json?

转载 作者:行者123 更新时间:2023-11-29 20:03:19 26 4
gpt4 key购买 nike

所以我有一个 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

有人知道我怎样才能把它捡起来吗?

最佳答案

您的 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"
}
]

关于java - 如何在 Android 上用 Java 解析格式错误的 Json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35959754/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com