当前位置:网站首页>Handle double quotation mark escape in JSON string

Handle double quotation mark escape in JSON string

2022-06-11 05:23:00 Bug repair robot

1. The string passed from the foreground , adopt JSON.parseArray Turn into a collection

import com.alibaba.fastjson.JSON;
List<Subject> subjectList = JSON.parseArray(project.getSubjectStr(), Subject.class);

2. The double quotation marks contained in the data will cause the conversion to fail

3. Method of escape

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

4. Then switch

5. Can be saved , Escape from the front end Or it can be parsed and converted into double quotation marks

String  way=way.replace('”','"');

原网站

版权声明
本文为[Bug repair robot]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020540292029.html