com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:第1行第118列未终止的对象

问题描述 投票:0回答:1

下面是我从 api 获取并登录 onSuccess 的 json 响应

[{attendanceWithTaskCount=[{InstallerPresent=3.0, TotalTaskDone=8.0}], installersDetails=[{InstallerName=abc- fjd, JobType=erret, TaskCount=6.0}]}]

下面是我的函数,其中从上面的 json 中提取数据并得到 json 格式错误的错误

public void installersAttendanceWithTaskCount(String date) {
        TaskController.getInstance(SupervisorTaskSummaryActivity.this).installersAttendanceWithTaskCount(SupervisorTaskSummaryActivity.this, String.valueOf(UserInfo.getUserInfo().getUserId()), date, new IActionCallback() {
            @Override
            public void onSuccess(Object data) {
                try {
                    Gson gson = new Gson();
                    Log.d("API response: ", String.valueOf(data));

                    // Parse the response JSON as an array
                    JsonArray jsonArray = gson.fromJson(String.valueOf(data), JsonArray.class);
                    Log.d("API jsonArray: ", String.valueOf(jsonArray));
                    if (jsonArray.size() > 0) {
                        // Extract the first object from the array
                        JsonObject jsonObject = jsonArray.get(0).getAsJsonObject();

                        // Extract data from "installersDetails" array
                        JsonArray installersArray = jsonObject.getAsJsonArray("installersDetails");

                        if (installersArray.size() > 0) {
                            installerAttendanceWithTaskCountList = new ArrayList<>();

                            for (JsonElement item : installersArray) {
                                if (item.isJsonObject()) {
                                    JsonObject taskSummary = item.getAsJsonObject();
                                    String InstallerName = taskSummary.get("InstallerName").getAsString();

                                    // Replace spaces in the InstallerName field with underscores
                                    //InstallerName = InstallerName.replaceAll(" ", "_");

                                    String JobType = taskSummary.get("JobType").getAsString();
                                    Double TaskCount = taskSummary.get("TaskCount").getAsDouble();

                                    installerAttendanceWithTaskCountModel model = new installerAttendanceWithTaskCountModel(InstallerName, JobType, TaskCount);
                                    installerAttendanceWithTaskCountList.add(model);
                                }
                            }

                            // Creating RecyclerView adapter and setting it
                            installerAttendaceWithTaskCountAdapter adapter = new installerAttendaceWithTaskCountAdapter(SupervisorTaskSummaryActivity.this, installerAttendanceWithTaskCountList);
                            recycler_view.setAdapter(adapter);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int errorCode, final String errorMessage) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(SupervisorTaskSummaryActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

现在我遇到以下错误

    com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 118 path $[0].installersDetails[0].InstallerName
2023-09-27 17:01:03.416 22744-22744 System.err                              W      at com.google.gson.internal.Streams.parse(Streams.java:56)
2023-09-27 17:01:03.416 22744-22744 System.err                              W      at com.google.gson.JsonParser.parse(JsonParser.java:84)
2023-09-27 17:01:03.416 22744-22744 System.err                              W      at com.google.gson.JsonParser.parse(JsonParser.java:59)
2023-09-27 17:01:03.416 22744-22744 System.err                              W      at com.google.gson.JsonParser.parse(JsonParser.java:45)
2023-09-27 17:01:03.417 22744-22744 System.err                              W      at .view.SupervisorTaskSummary.SupervisorTaskSummaryActivity$2.onSuccess(SupervisorTaskSummaryActivity.java:135)
2023-09-27 17:01:03.417 22744-22744 System.err                              W      at .coreengine.controllers.TaskController$30.onSuccess(TaskController.java:3229)
2023-09-27 17:01:03.417 22744-22744 System.err                              W      at .coreengine.controllers.TaskController$30.onSuccess(TaskController.java:3223)
2023-09-27 17:01:03.417 22744-22744 System.err                              W      at .coreengine.webapi.CommProvider$1.onResponse(CommProvider.java:118)
2023-09-27 17:01:03.417 22744-22744 System.err                              W      at .coreengine.webapi.GsonRequest.deliverResponse(GsonRequest.java:120)
2023-09-27 17:01:03.417 22744-22744 System.err                              W      at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
2023-09-27 17:01:03.417 22744-22744 System.err                              W      at android.os.Handler.handleCallback(Handler.java:942)
2023-09-27 17:01:03.417 22744-22744 System.err                              W      at android.os.Handler.dispatchMessage(Handler.java:99)
2023-09-27 17:01:03.417 22744-22744 System.err                              W      at android.os.Looper.loopOnce(Looper.java:223)
2023-09-27 17:01:03.417 22744-22744 System.err                              W      at android.os.Looper.loop(Looper.java:324)
2023-09-27 17:01:03.417 22744-22744 System.err                              W      at android.app.ActivityThread.main(ActivityThread.java:8385)
2023-09-27 17:01:03.417 22744-22744 System.err                              W      at java.lang.reflect.Method.invoke(Native Method)
2023-09-27 17:01:03.418 22744-22744 System.err                              W      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:582)
2023-09-27 17:01:03.418 22744-22744 System.err                              W      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1059)
2023-09-27 17:01:03.418 22744-22744 System.err                              W  Caused by: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 118 path $[0].installersDetails[0].InstallerName
2023-09-27 17:01:03.418 22744-22744 System.err                              W      at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1573)
2023-09-27 17:01:03.418 22744-22744 System.err                              W      at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:495)
2023-09-27 17:01:03.418 22744-22744 System.err                              W      at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:418)
2023-09-27 17:01:03.418 22744-22744 System.err                              W      at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:666)
2023-09-27 17:01:03.418 22744-22744 System.err                              W      at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:659)
2023-09-27 17:01:03.418 22744-22744 System.err                              W      at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:667)
2023-09-27 17:01:03.418 22744-22744 System.err                              W      at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:659)
2023-09-27 17:01:03.418 22744-22744 System.err                              W      at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:642)
2023-09-27 17:01:03.419 22744-22744 System.err                              W      at com.google.gson.internal.Streams.parse(Streams.java:44)
2023-09-27 17:01:03.419 22744-22744 System.err                              W      ... 17 more

我检查了从 api 返回的 json 是完美的,但出现了上述错误。请帮忙解决这个问题

android json gson
1个回答
0
投票

您提供的 JSON 无效。请考虑将“=”替换为“:”并向字符串值添加引号。这是修改后的版本:

[{
    "attendanceWithTaskCount": [{
        "InstallerPresent": 3.0,
        "TotalTaskDone": 8.0
    }],
    "installersDetails": [{
        "InstallerName": "abc- fjd",
        "JobType": "erret",
        "TaskCount": 6.0
    }]
}]
© www.soinside.com 2019 - 2024. All rights reserved.