今天通过阿里云的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)