分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

JSON解析值富文本

发布时间:2023-09-06 02:22责任编辑:顾先生关键词:暂无标签

解析前端传递的JSON数据中可能如下

{ "result": "<input value="Test" type="button" onclick="alert(""OK"");" />", "msg": "test"} 

此时去解析是无法解析出来的,存在 / 空格 多的双引号,

参考多个结果

针对双引号(利用中文双引号代替多余的英文双引号后去解析JSON串)

public String jsonStringConvert(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); ???}

针对空格(先调用此方法)

public String replaceBlank(String str) { ???????String dest = ""; ???????if (str != null) { ???????????Pattern p = Pattern.compile("\\s*|\t|\r|\n"); ???????????Matcher m = p.matcher(str); ???????????dest = m.replaceAll(""); ???????????// Pattern p2 = Pattern.compile("\\s*\""); ???????????// Matcher m2 = p2.matcher(dest); ???????????// dest = m2.replaceAll("\‘"); ???????????dest = dest.replace("=\"", "=‘"); ???????????p = Pattern.compile("\"\0*>"); ???????????m = p.matcher(dest); ???????????dest = m.replaceAll(">‘"); ???????} ???????return dest; ???}

以上会造成数据的格式少了空格,需要自己去添加上(不能很好的解决问题)

对双引号进行转译

对富文本加密,后台解密存储

参考:https://blog.csdn.net/jbb0403/article/details/45918693

  :

JSON解析值富文本

原文地址:https://www.cnblogs.com/oldzhang1222/p/9953738.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved