美文网首页
Java Json数据中有双引号"未转义的解析报错

Java Json数据中有双引号"未转义的解析报错

作者: 魏树鑫 | 来源:发表于2019-05-14 10:24 被阅读0次

    例如Json数据中有双引号,解析时会报Json格式错误或者转义有问题

    {
      "content": "重要任务提醒",
      "matterName": ""德勤57"装砂滞留船期损失案"
    }
    

    可以在解析前现将Json格式化一遍,这里面主要是双引号匹配的查找

     public static String formatErrorJson(String s) {
            char[] temp = s.toCharArray();
            int n = temp.length;
            for (int i = 0; i < n; i++) {
                if (temp[i] == ':' && temp[i + 1] == '"') {
                    for (int j = i + 2; j < n; j++) {
                        if (temp[j] == '"') {
                            if (temp[j + 1] != ',' && temp[j + 1] != '}') {
                                temp[j] = '”';
                            } else if (temp[j + 1] == ',' || temp[j + 1] == '}') {
                                break;
                            }
                        }
                    }
                }
            }
            return new String(temp);
    }
    

    相关文章

      网友评论

          本文标题:Java Json数据中有双引号"未转义的解析报错

          本文链接:https://www.haomeiwen.com/subject/mkijaqtx.html