javascript 使用节点将字符串解析为 JSON 给出了意外的令牌,验证器说没问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22206426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing string to JSON using node gives unexpected token, validator says ok
提问by Patryk
I have the following string that I would like to parse to JSON :
我有以下字符串要解析为 JSON :
{
"STATUS": [
{
"STATUS": "S",
"When": 1394044643,
"Code": 17,
"Msg": "GPU0",
"Description": "cgminer 3.7.3"
}
],
"GPU": [
{
"GPU": 0,
"Enabled": "Y",
"Status": "Alive",
"Temperature": 70,
"Fan Speed": 3089,
"Fan Percent": 70,
"GPU Clock": 1180,
"Memory Clock": 1500,
"GPU Voltage": 1.206,
"GPU Activity": 99,
"Powertune": 20,
"MHS av": 0.4999,
"MHS 5s": 0.5009,
"Accepted": 4335,
"Rejected": 7,
"Hardware Errors": 0,
"Utility": 27.8007,
"Intensity": "0",
"Last Share Pool": 0,
"Last Share Time": 1394044643,
"Total MH": 4676.7258,
"Diff1 Work": 69436,
"Difficulty Accepted": 69360,
"Difficulty Rejected": 112,
"Last Share Difficulty": 16,
"Last Valid Work": 1394044643,
"Device Hardware%": 0,
"Device Rejected%": 0.1613,
"Device Elapsed": 9356
}
],
"id": 1
}
When I use e.g. http://jsonlint.com/it says that the JSON is correct but when I use in node.js:
当我使用例如http://jsonlint.com/ 时,它说 JSON 是正确的,但是当我在 node.js 中使用时:
console.log(JSON.parse(data.toString()));
I get the following :
我得到以下信息:
undefined:1
e Hardware%":0.0000,"Device Rejected%":0.1570,"Device Elapsed":9554}],"id":1}
^
SyntaxError: Unexpected token
Any clue what am I doing wrong here ?
任何线索我在这里做错了什么?
EDIT
编辑
The data is coming as ByteStream :
数据以 ByteStream 的形式出现:
.on('data',function(data){
console.log(data.toString());
console.log();
console.log(data);
console.log();
console.log("data "+ data.GPU);
//...
//...
{"STATUS":[{"STATUS":"S","When":1394045650,"Code":17,"Msg":"GPU0","Description":"cgminer 3.7.3"}],"GPU":[{"GPU":0,"Enabled":"Y","Status":"Alive","Temperature":70.00,"Fan Speed":3090,"Fan Percent":70,"GPU Clock":1180,"Memory Clock":1500,"GPU Voltage":1.206,"GPU Activity":99,"Powertune":20,"MHS av":0.4999,"MHS 5s":0.5007,"Accepted":4841,"Rejected":8,"Hardware Errors":0,"Utility":28.0261,"Intensity":"0","Last Share Pool":0,"Last Share Time":1394045638,"Total MH":5181.3734,"Diff1 Work":77548,"Difficulty Accepted":77456.00000000,"Difficulty Rejected":128.00000000,"Last Share Difficulty":16.00000000,"Last Valid Work":1394045638,"Device Hardware%":0.0000,"Device Rejected%":0.1651,"Device Elapsed":10364}],"id":1}
<Buffer 7b 22 53 54 41 54 55 53 22 3a 5b 7b 22 53 54 41 54 55 53 22 3a 22 53 22 2c 22 57 68 65 6e 22 3a 31 33 39 34 30 34 35 32 34 38 2c 22 43 6f 64 65 22 3a 31 ...>
data undefined
EDIT
编辑
When I do this :
当我这样做时:
console.log(data.toString());
console.log(JSON.stringify(data.toString()));
I get the following result :
我得到以下结果:
"{"STATUS":[{"STATUS":"S","When":1394046864,"Code":17,"Msg":"GPU0","Description":"cgminer 3.7.3"}],"GPU":[{"GPU":0,"Enabled":"Y","Status":"Alive","Temperature":70.00,"Fan Speed":3087,"Fan Percent":70,"GPU Clock":1180,"Memory Clock":1500,"GPU Voltage":1.206,"GPU Activity":99,"Powertune":20,"MHS av":0.5000,"MHS 5s":0.5016,"Accepted":5396,"Rejected":8,"Hardware Errors":0,"Utility":27.9597,"Intensity":"0","Last Share Pool":0,"Last Share Time":1394046864,"Total MH":5789.2352,"Diff1 Work":86428,"Difficulty Accepted":86336.00000000,"Difficulty Rejected":128.00000000,"Last Share Difficulty":16.00000000,"Last Valid Work
":1394046864,"Device Hardware%":0.0000,"Device Rejected%":0.1481,"Device Elapsed":11580}],"id":1}"
"{\"STATUS\":[{\"STATUS\":\"S\",\"When\":1394046864,\"Code\":17,\"Msg\":\"GPU0\",\"Description\":\"cgminer 3.7.3\"}],\"GPU\":[{\"GPU\":0,\"Enabled\":\"Y\",\"Status\":\"Alive\",\"Temperature\":70.00,\"Fan Sp
eed\":3087,\"Fan Percent\":70,\"GPU Clock\":1180,\"Memory Clock\":1500,\"GPU Voltage\":1.206,\"GPU Activity\":99,\"Powertune\":20,\"MHS av\":0.5000,\"MHS 5s\":0.5016,\"Accepted\":5396,\"Rejected\":8,\"Hardw
are Errors\":0,\"Utility\":27.9597,\"Intensity\":\"0\",\"Last Share Pool\":0,\"Last Share Time\":1394046864,\"Total MH\":5789.2352,\"Diff1 Work\":86428,\"Difficulty Accepted\":86336.00000000,\"Difficulty Re
jected\":128.00000000,\"Last Share Difficulty\":16.00000000,\"Last Valid Work\":1394046864,\"Device Hardware%\":0.0000,\"Device Rejected%\":0.1481,\"Device Elapsed\":11580}],\"id\":1}\u0000"
Notice the last unicode character of the second message \u0000
what can I do with it?
请注意第二条消息的最后一个 unicode 字符,\u0000
我可以用它做什么?
回答by Patryk
The problem was with the terminating null character. After removing it I can now parse the string no problemo ( post more efficient way if you have it)
问题在于终止的空字符。删除它后,我现在可以毫无问题地解析字符串(如果有,请发布更有效的方法)
var re = /var str = data.toString().slice(0, - 1);
/g;
str = data.toString().replace(re, "");
var o = JSON.parse(str);
console.log(o);
or
或者
var buf = data.slice(0,data.length-1);
console.log(JSON.parse(buf.toString()));
or work directly on bytes in Buffer
或直接处理字节 Buffer
var a = '{"STATUS":[{"STATUS":"S","When":1394045650,"Code":17,"Msg":"GPU0","Description":"cgminer 3.7.3"}],"GPU":[{"GPU":0,"Enabled":"Y","Status":"Alive","Temperature":70.00,"Fan Speed":3090,"Fan Percent":70,"GPU Clock":1180,"Memory Clock":1500,"GPU Voltage":1.206,"GPU Activity":99,"Powertune":20,"MHS av":0.4999,"MHS 5s":0.5007,"Accepted":4841,"Rejected":8,"Hardware Errors":0,"Utility":28.0261,"Intensity":"0","Last Share Pool":0,"Last Share Time":1394045638,"Total MH":5181.3734,"Diff1 Work":77548,"Difficulty Accepted":77456.00000000,"Difficulty Rejected":128.00000000,"Last Share Difficulty":16.00000000,"Last Valid Work":1394045638,"Device Hardware%":0.0000,"Device Rejected%":0.1651,"Device Elapsed":10364}],"id":1}';
var b = new Buffer(a);
console.log(JSON.parse(b.toString());
回答by Mukesh Soni
I copied your console log output for data.toString() and and as @patryk pointed out, there is a unicode character \u0000 at the end of the string. That might be causing the problem. I removed the trailing character, assigned the string to a variable and created a buffer out of it and everything is hunky dory.
我复制了 data.toString() 的控制台日志输出,正如@patryk 指出的那样,字符串末尾有一个 unicode 字符 \u0000。这可能是导致问题的原因。我删除了尾随字符,将字符串分配给一个变量并从中创建了一个缓冲区,一切都很好。
{ STATUS:
[ { STATUS: 'S',
When: 1394045650,
Code: 17,
Msg: 'GPU0',
Description: 'cgminer 3.7.3' } ],
GPU:
[ { GPU: 0,
Enabled: 'Y',
Status: 'Alive',
Temperature: 70,
'Fan Speed': 3090,
'Fan Percent': 70,
'GPU Clock': 1180,
'Memory Clock': 1500,
'GPU Voltage': 1.206,
'GPU Activity': 99,
Powertune: 20,
'MHS av': 0.4999,
'MHS 5s': 0.5007,
Accepted: 4841,
Rejected: 8,
'Hardware Errors': 0,
Utility: 28.0261,
Intensity: '0',
'Last Share Pool': 0,
'Last Share Time': 1394045638,
'Total MH': 5181.3734,
'Diff1 Work': 77548,
'Difficulty Accepted': 77456,
'Difficulty Rejected': 128,
'Last Share Difficulty': 16,
'Last Valid Work': 1394045638,
'Device Hardware%': 0,
'Device Rejected%': 0.1651,
'Device Elapsed': 10364 } ],
id: 1 }
output-
输出-
##代码##