JSON转换出现错误:net.sf.json.JSONException: Unterminated string at character 38 of
来源:互联网 发布:网络羽绒服品牌排行榜 编辑:程序博客网 时间:2024/06/05 08:01
在项目中使用了文本编辑器,在做添加和修改操作时,经常会把各个输入表单域的值组装成json,然后使用ajax进行form表单提交。而文本编辑器提交的值是html标签的格式:如下:
<p style="font-size:20px;">你好, </p>
在提交到服务器使用JSONObject.fromObject(param)
时会报
net.sf.json.JSONException: Unterminated string at character 38 of
错误,解决该方法,我是这样做的。虽然提交到后台的数据是html标签格式的,在调用JSONObject.fromObject(param)
之前,通过转换处理<,>和单引号和双引号等特殊字符,然后Post到服务器后,再解码成正常的,然后继续保存或修改到数据库里。
/*3.用正则表达式实现html转码*/ private String htmlEncodeByRegExp(String str){ String s = ""; if(str.length() == 0) return ""; s = str.replace("&","&"); s = s.replace("<","<"); s = s.replace(">",">"); s = s.replace("'","'"); s = s.replace("\"","""); s = s.replace("\n","\\\\n"); s = s.replace("\r","\\\\r"); return s; }
/*4.用正则表达式实现html解码*/ private String htmlDecodeByRegExp (String str){ String s = ""; if(str.length() == 0) return ""; s = str.replace("&","&"); s = s.replace("<","<"); s = s.replace(">",">"); s = s.replace(" "," "); s = s.replace("'","\'"); s = s.replace(""","\""); s = s.replace("\\n", "\n"); s = s.replace("\\r", "\r"); return s; }
这样就绕开了JSON转换的错误,我也是网上找到的方法,希望对各位小伙伴有所帮助!
阅读全文
0 0
- JSON转换出现错误:net.sf.json.JSONException: Unterminated string at character 38 of
- JSON转换出现错误:net.sf.json.JSONException: Unterminated string at character 38 of
- net.sf.json.JSONException: Unterminated string at character 2101
- net.sf.json.JSONException: Unterminated string at character 1801
- JSON经典异常:org.json.JSONException: Unterminated string at character
- JSON经典异常:org.json.JSONException: Unterminated string at character
- org.json.JSONException: Unterminated object at character 97 of
- net.sf.json.JSONException: Found starting '{' but missing '}' at the end. at character 0 of null
- net.sf.json.JSONException: Expected a ',' or '}' at character 65 of {"totalCount":343,"data":
- net.sf.json.JSONException: Expected a ',' or '}' at character 546 of
- Android异常汇集----2. org.json.JSONException: Unterminated string at character
- net.sf.json.JSONException: Unquotted string "E44C2B0168A550F6503F59CCD56FC49B"
- net.sf.json.JSONException
- org.json.JSONException: End of input at character 0 of
- org.json.JSONException: End of input at character 0 of
- org.json.JSONException:End of input at character 0 of
- net.at.json.JSONException
- java的date类型无法读取 出现错误net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
- div布局,哈哈有点丑
- ubuntu16.04 安装 opencv2.4.9
- matlab查看帮助文档
- LeetCode 之 Single Number
- 节点发布和订阅
- JSON转换出现错误:net.sf.json.JSONException: Unterminated string at character 38 of
- Jzoj5445【NOIP2017提高A组冲刺11.2】失格
- html基础标签笔记
- 在清单列表需要加入的权限
- SoftmaxWithLossLayer 详细讲解
- d3, geoJson 网站分享
- 洛谷 2285 打鼹鼠 递推? DP? 解题报告
- LeetCode718. Maximum Length of Repeated Subarray(Medium)
- 虔诚的墓主人 洛谷p2154