golang处理 json中非法字符
2015-03-12 10:58
1101 查看
原文:
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:
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 解析就会报错。
于是有个哥们就出了一招。
========================
If you don't mind them being replaced with spaces, you could do something like:
~K
========================================
具体什么原理呢?还不清楚。
大概是根据字符集来判断哪些是json里面的非法字符,然后替换成空格。
计算机,字符集。。。。。后面需要研究一下了。
===================================
看一下ASCII可显示字符表,就明白了。 空格开始,~结束。加上\r\n\t这3个字符。
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 解析就会报错。
于是有个哥们就出了一招。
========================
If you don't mind them being replaced with spaces, you could do something like:
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] = ' ' } }
~K
========================================
具体什么原理呢?还不清楚。
大概是根据字符集来判断哪些是json里面的非法字符,然后替换成空格。
计算机,字符集。。。。。后面需要研究一下了。
===================================
看一下ASCII可显示字符表,就明白了。 空格开始,~结束。加上\r\n\t这3个字符。
ASCII可显示字符
|
|
|
相关文章推荐
- 【玩转Golang】 自定义json序列化对象时,非法字符错误原因
- JSON文件中非法字符的处理
- 如何处理 TextBox 里的非法字符
- xml中的非法字符处理
- 如何处理JSON中的特殊字符
- JSON 特殊字符处理
- 如何处理JSON中的特殊字符
- c中的非法字符的处理
- Json特殊字符处理
- 如何处理JSON中的特殊字符
- java 中JSON数据特殊字符的处理
- java 中JSON数据特殊字符的处理
- 利用 Newtonsoft 处理Json 特殊字符
- 处理输入框中输入非法字符的办法,将输入的非法字符转化成合法字符。
- java 中JSON数据特殊字符的处理
- JSON中数据特殊字符的处理
- json字符中的特殊字符处理
- JSON 特殊字符处理
- json字符串中的特殊字符处理
- java 中JSON数据特殊字符的处理