golang处理 json中非法字符
原文:
Hi there,
I just discovered Go and decided to port a little program to Go.
The program reads JSON-Data from an URL and process the Data. The Go
port works well till now.
I dont have any influence on the JSON data and so sometimes there are
control character in it and my program crashes with "invalid character
'\x12' in string literal"
here the code sample of my program:
http_return, err := http.Get(newurl)
var http_body []byte;
if err == nil {
http_body, err = ioutil.ReadAll(http_return.Body)
http_return.Body.Close()
}
param_info := make(map[string]interface{})
param_err := json.Unmarshal(http_body, ¶m_info)
This Unmarshal call crashes with "invalid character '\x12' in string
literal"
How do I remove this or any similar control character from the JSON
data. I dont need those characters so they can simply be removed.
Thanks
Nico
==============================================================
意思大概就是说,json是通过网络传输或者其它方法获取的,然后里面可能包含特殊字符,这样呢,在go里面用json 解析就会报错。
于是有个哥们就出了一招。
========================
for i, ch := range http_body {
switch {
case ch > '~': http_body[i] = ' '
case ch == '\r':
case ch == '\n':
case ch == '\t':
case ch < ' ': http_body[i] = ' '
}
}
ASCII可显示字符
|
|
|
golang处理 json中非法字符的更多相关文章
- url 中非法字符替换,java 正则替换
url在传输时不允许的一些字符串,参考自:http://www.ietf.org/rfc/rfc1738.txt 以下字符用java正则替换为"_",一句话搞定: "{& ...
- 检测ADO.net拼接字符串中非法字符
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Refle ...
- SVN Files 的值“ < < < < < < < .mine”无效。路径中具有非法字符。
错误 1 Files 的值“ < < < < < < < .mine”无效.路径中具有非法字符. 今天使用SVN进行更新的时候,出现了如上问题,想起卓 ...
- xml中的非法字符
今使用Jdom生成xml文件的时候,总是出现0x0,0x8为非法字符,经过搜索,问题原因及解决方法如下: 原因:xml中需要过滤的字符分为两类,一类是不允许出现在xml中的字符,这些字符不在xml的定 ...
- XML中的非法字符转化成实体
问题 如果XML有非法字符比如 "·",或者HTML标签<br/>.XML在解析的过程中就会出错.就无法正常解析,或者把xml反射成实体. 有些字符,像(<)这类 ...
- 错误 1 Files 的值“ < < < < < < < .mine”无效。路径中具有非法字符。
错误 1 Files 的值“ < < < < < < < .mine”无效.路径中具有非法字符. 今天使用SVN进行更新的时候,出现了如上问题,想起卓 ...
- Android studio中出现非法字符时的部分解决方法
我将原来在Eclipse中开发的工程搬到了Android studio上来,在运行,编译程序的过程中出现了错误.提示存在非法字符. 后来发现是由于程序代码中有中文字符的出现,问题就出在对中文支持的UT ...
- php验证输入字符串中含有非法字符
$pattern = "/(&|"|<|>|')+/"; preg_match($pattern, $media_name, $matches); ...
- SVN提交出现“< < < < < < < .mine’无效,路径中具有非法字符”的问题
使用SVN提交或更新后经常会出现”Files 的值’< < < < < < < .mine’无效.路径中具有非法字符”的错误.查阅了下资料,是因为:你更改了一 ...
随机推荐
- JSON特殊字符的处理
本文转自:http://blog.csdn.net/btt2013/article/details/66970735 JSon 数据中的String 传递数据时,需要处理好特殊字符. JSon数据中有 ...
- python操作word(改课文格式)【最终版】
python操作word的一些方法,前面写了一些感悟,有点跑题,改了下题目,方便能搜索到.心急的可以直接拉到最后看代码,我都加了比较详细的注释. 从8.3号早上9点,到8.8号下午5点半下班,终于把这 ...
- renderer:function参数介绍
转载自:http://blog.sina.com.cn/s/blog_9eaf28f90101b7y3.html renderer:function(value, cellmeta, record, ...
- 编译hadoop,spark遇到的问题总结
编译hadoop2.6.4 1.JDK8版本过高,换成JDK7: 2.换成命令行mvn package -Pdist,native -DskipTests-Dtar-Dmaven.javadoc.sk ...
- php5共存php7
PHP7与PHP5共存于CentOS7 原文参考 原理 思路很简单:PHP5是通过yum安装的在/usr/,套接字在/var/run/php-fpm.socket,PHP7自己编译装在/usr/loc ...
- NAT and Traversal NAT(TURN/STUN/ICE)
http://www.cnblogs.com/whyandinside/archive/2010/12/08/1900492.html -------------------------------- ...
- jquery checkbox勾选/取消
作者:@keenleung本文为作者原创,转载请注明出处:http://www.cnblogs.com/KeenLeung/p/3799895.html 目录 <form> 你爱好的运动是 ...
- sql one
查询的话 子查询什么的都很正常 添加的话 尽量把东西都添加在一个表单里 这是源头 有个这个方便的源头 查询和删除都会方便很多 组建一个网站,不可避免的要进行调试,有些功能需要添加或者删除,对于后台来讲 ...
- 一个区域只能放置一个组件,如果想在一个区域放置多个组件就需要使用Panel来装载
五种布局管理器: Flow Layout(流式布局):按照组件添加到容器中的顺序,顺序排放组件位置.默认为水平排列,如果越界那么会向下排列.排列的位置随着容器大小的改变而改变. Panel默认的布局管 ...
- css -- hover伪类
CSS代码: .btn-setDefaultGateway{display: none;} .netDiv:hover span .btn-setDefaultGateway { display: i ...