使用节点将字符串解析为JSON会产生意外的令牌,验证器说好

时间:2014-03-05 18:41:18

标签: javascript json node.js parsing

我有以下要解析为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
}

当我使用例如http://jsonlint.com/它说JSON是正确的,但是当我在node.js中使用时:

console.log(JSON.parse(data.toString()));

我得到以下内容:

undefined:1
e Hardware%":0.0000,"Device Rejected%":0.1570,"Device Elapsed":9554}],"id":1}
                                                                             ^
SyntaxError: Unexpected token

任何线索我在这里做错了什么?

修改

数据来自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

编辑

当我这样做时:

console.log(data.toString());
console.log(JSON.stringify(data.toString()));

我得到以下结果:

"{"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"

注意第二条消息的最后一个unicode字符\u0000我该怎么办?

2 个答案:

答案 0 :(得分:10)

问题在于终止空字符。删除后,我现在可以解析字符串没有问题(如果你有更高效的方式发布)

var re = /\0/g;
str = data.toString().replace(re, "");
var o = JSON.parse(str);
console.log(o);

var str = data.toString().slice(0, - 1);

或直接处理Buffer

中的字节
var buf = data.slice(0,data.length-1);
console.log(JSON.parse(buf.toString()));

答案 1 :(得分:3)

我复制了data.toString()的控制台日志输出,并且正如@patryk指出的那样,字符串末尾有一个unicode字符\ u0000。这可能会导致问题。我删除了尾随字符,将字符串分配给变量并从中创建了一个缓冲区,所有东西都是hunky dory。

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());

输出 -

{ 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 }