添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
好帅的小熊猫  ·  Spring ...·  4 月前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 118

Ask Question

Below is my json response getting from my api and getting in log in onSuccess

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

below is my function in which extracting data from above json and getting json malformed error

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

Now I am getting below error

    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

I have checked my json returning from api is perfect but getting above error. Please help in resolving this issue

The JSON you provided is not valid. Please consider replacing '=' with ':' and adding quotes to string values. Here is the revised version:

"attendanceWithTaskCount": [{ "InstallerPresent": 3.0, "TotalTaskDone": 8.0 "installersDetails": [{ "InstallerName": "abc- fjd", "JobType": "erret", "TaskCount": 6.0 if you're using Retrofit for networking, probably it's easier to use a Converter.Factory with addConverterFactory in this case – Vlad Guriev Sep 27, 2023 at 13:37

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.