public static void main(String[] args) {
String str="{\"productid2\":26454950,\"productname2\":\"一房一厅园景大床房(5号楼)3晚(可拆分)+\"途\"书馆阅览3份\"}";
// 创建JSON对象
// 获取键值对应的值
String str2=toJsonString(str);
String modifiedJson = str.replace("\"", "\\\"");;
String modifiedJson1 = modifiedJson.replace("@@DOUBLE_QUOTE@@", "\"");
Map<String,String> paramMap = JacksonUtil.toObject(str2, Map.class);
System.out.println( );
}
// 处理json字符串中value多余的双引号, 将多余的双引号替换为中文双引号
private static String toJsonString(String s) {
char[] tempArr = s.toCharArray();
int tempLength = tempArr.length;
for (int i = 0; i < tempLength; i++) {
if (tempArr[i] == ':' && tempArr[i + 1] == '"') {
for (int j = i + 2; j < tempLength; j++) {
if (tempArr[j] == '"') {
if (tempArr[j + 1] != ',' && tempArr[j + 1] != '}') {
tempArr[j] = '”'; // 将value中的 双引号替换为中文双引号
} else if (tempArr[j + 1] == ',' || tempArr[j + 1] == '}') {
break;
}
}
}
}
}
return new String(tempArr);
}
字符串套字符串解析json
于 2024-01-10 16:22:37 首次发布