Gson解析Json数据内有非法字符("",回车,etc)时的解决办法

来源:互联网 发布:centos simsun.ttc 编辑:程序博客网 时间:2024/05/17 23:16

如果所示,json数据内有回车,这时候用Gson解析是会出现错错误的

如图所示


可以进行如下操作(其中content是完整的json数据)

  1. 转义回车
    转移回车
  2. 转义换行
    这里写图片描述
  3. 其他如此类推

还有一个关键的地方,如果json的value值中有”“,也是会出现解析错误的,那么我们可以将”“转变为中文的双引号

将英文引号转为中文的引号方法

private static String jsonString(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);    }
0 0
原创粉丝点击