您的位置:首页 > Web前端 > JavaScript

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:

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可显示字符

二进制十进制十六进制图形
0010 00003220(空格)(␠)
0010 00013321!
0010 00103422"
0010 00113523#
0010 01003624$
0010 01013725%
0010 01103826&
0010 01113927'
0010 10004028(
0010 10014129)
0010 1010422A*
0010 1011432B+
0010 1100442C,
0010 1101452D-
0010 1110462E.
0010 1111472F/
0011 000048300
0011 000149311
0011 001050322
0011 001151333
0011 010052344
0011 010153355
0011 011054366
0011 011155377
0011 100056388
0011 100157399
0011 1010583A:
0011 1011593B;
0011 1100603C<
0011 1101613D=
0011 1110623E>
0011 1111633F?
二进制十进制十六进制图形
0100 00006440@
0100 00016541A
0100 00106642B
0100 00116743C
0100 01006844D
0100 01016945E
0100 01107046F
0100 01117147G
0100 10007248H
0100 10017349I
0100 1010744AJ
0100 1011754BK
0100 1100764CL
0100 1101774DM
0100 1110784EN
0100 1111794FO
0101 00008050P
0101 00018151Q
0101 00108252R
0101 00118353S
0101 01008454T
0101 01018555U
0101 01108656V
0101 01118757W
0101 10008858X
0101 10018959Y
0101 1010905AZ
0101 1011915B[
0101 1100925C\
0101 1101935D]
0101 1110945E^
0101 1111955F_
二进制十进制十六进制图形
0110 00009660`
0110 00019761a
0110 00109862b
0110 00119963c
0110 010010064d
0110 010110165e
0110 011010266f
0110 011110367g
0110 100010468h
0110 100110569i
0110 10101066Aj
0110 10111076Bk
0110 11001086Cl
0110 11011096Dm
0110 11101106En
0110 11111116Fo
0111 000011270p
0111 000111371q
0111 001011472r
0111 001111573s
0111 010011674t
0111 010111775u
0111 011011876v
0111 011111977w
0111 100012078x
0111 100112179y
0111 10101227Az
0111 10111237B{
0111 11001247C|
0111 11011257D}
0111 11101267E~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: