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’无效.路径中具有非法字符”的错误.查阅了下资料,是因为:你更改了一 ...
随机推荐
- scala flatMap reduceLeft foldLeft
object collection_t1 { def flatMap1(): Unit = { val li = List(,,) val res = li.flatMap(x => x mat ...
- shell监控脚本,不考虑多用户情况
#!/bin/bash CheckProcess() { if [ "$1" = "" ]; then fi PROCESS_NUM=`ps -ef | gre ...
- catch signal
捕抓信号 如果信号的处理动作是用户自定义函数,在信号递达时就调用这个函数,称为捕抓信号. 除了SIGSTOP和SIGKILL进程能够忽略或捕获其他的全部信号. 注:信号可从两个不同分类角度对信号进行分 ...
- 浅谈NFC、RFID、红外、蓝牙的区别
很多朋友对NFC和RFID这两个词感到陌生,但是手机经常会出现支持NFC支付,又没太在意,NFC与RFID其实是手机支付的种方式(手机支付也被称作移动支付,是一种允许移动用户使用其移动终端对所消费的商 ...
- 如何解决PHP里大量数据循环时内存耗尽的问题
最近在开发一个PHP程序时遇到了下面的错误: PHP Fatal error: Allowed memory size of 268 435 456 bytes exhausted 错误信息显示允许的 ...
- 【UVa】Partitioning by Palindromes(dp)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=27&page=sh ...
- Codeforces Round #265 (Div. 2)
http://codeforces.com/contest/465 rating+7,,简直... 感人肺腑...............蒟蒻就是蒟蒻......... 被虐瞎 a:inc ARG 题 ...
- oracle和SQLserver数据库中select into 的区别
在Oracle中是这样的 在SQLserver中是这样的
- Oracel 数据库面试题
1.取出表中第31到40行的记录mysql方案: , oracle方案: select t2.* ) t2 2.truncate和delete有什么区别TRUNCATE TABLE在功能上与不带WHE ...
- SHGetSpecialFolderPath用法
The SHGetSpecialFolderPath function retrieves the path of a special folder that is identified by its ...