1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 处理json字符串中双引号转义

处理json字符串中双引号转义

时间:2021-08-09 22:09:05

相关推荐

处理json字符串中双引号转义

1.前台传过来的字符串,通过JSON.parseArray转为集合

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

2.数据里面包含的双引号会导致转换失败

3.进行转义的的方法

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.再进行转换

5.可以进行保存,前端进行转义 或者解析出来转换为双引号

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。