在Java中修复json数据,json格式不正确,如何在Java中修复不正确的json

在Java中修复json数据,json格式不正确,如何在Java中修复不正确的json

渡星河
2023-04-15 / 0 评论 / 114 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2023年04月15日,已超过519天没有更新,若内容或图片失效,请留言反馈。

今天通过阿里云的orc服务写身份证识别功能,好家伙当我获取body部分后发现json格式不正确,这就麻烦了,不正确我获取名字啊,身份证什么的就麻烦,我就想着去修复一下,后面发现可以直接使用gson来进行修复

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.JsonObject;

String incorrectJson = "{name: \"John Doe\", age: 30, email: \"john.doe@example.com\"}";
System.out.println("Incorrect JSON: " + incorrectJson);

// 使用Gson库来修复JSON格式
Gson gson = new Gson();
JsonObject jsonObject = null;
try {
    jsonObject = gson.fromJson(incorrectJson, JsonObject.class);
} catch (JsonSyntaxException e) {
    System.out.println("Error: " + e.getMessage());
}

// 将修复后的JSON转换为字符串
String correctJson = gson.toJson(jsonObject);
System.out.println("Correct JSON: " + correctJson);
总结就是好用
0

评论 (0)

取消