提问人:Moin Khan 提问时间:9/27/2023 最后编辑:Moin Khan 更新时间:9/27/2023 访问量:63
com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:第 1 行第 118 列处的未终止对象
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 118
问:
以下是我从我的 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 是否完美,但出现上述错误。请帮助解决此问题
答:
0赞
Vlad Guriev
9/27/2023
#1
您提供的 JSON 无效。请考虑将“=”替换为“:”,并在字符串值中添加引号。以下是修订版本:
[{
"attendanceWithTaskCount": [{
"InstallerPresent": 3.0,
"TotalTaskDone": 8.0
}],
"installersDetails": [{
"InstallerName": "abc- fjd",
"JobType": "erret",
"TaskCount": 6.0
}]
}]
评论
0赞
Moin Khan
9/27/2023
它显示 =,因为这是一个 ResponseObject
0赞
Vlad Guriev
9/27/2023
您最初如何将 JSON 从响应转换为 inObject
IActionCallback
0赞
Moin Khan
9/27/2023
iCallback.onSuccess(response.getData())
0赞
Vlad Guriev
9/27/2023
如果您使用 Retrofit 进行网络,在这种情况下,将 Converter.Factory 与 addConverterFactory 一起使用可能更容易
0赞
Moin Khan
9/28/2023
我正在使用凌空抽射
评论