最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parsing string to JSON using node gives unexpected token, validator says ok - Stack Overflow

programmeradmin0浏览0评论

I have the following string that I would like to parse to 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. / it says that the JSON is correct but when I use in 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 :

.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?

I have the following string that I would like to parse to 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:

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 :

.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?

Share Improve this question edited Mar 5, 2014 at 19:16 Patryk asked Mar 5, 2014 at 18:41 PatrykPatryk 24.1k47 gold badges142 silver badges257 bronze badges 13
  • 1 data is already a JSON object. You're converting it to a String with toString() and then parsing it back to JSON immediately... Simply use data as-is. – André Dion Commented Mar 5, 2014 at 18:44
  • @AndréDion: If it's JSON, it's a string, though it could be a String object wrapper. Using .toString() is harmless. And JSON.parse doesn't turn it back into JSON. It takes the JSON and turns it into JavaScript objects. – cookie monster Commented Mar 5, 2014 at 18:46
  • @cookiemonster JSON is not a string. It's a stricter subset of an object literal. JSON.parse takes a String and converts to a true object literal. – André Dion Commented Mar 5, 2014 at 18:48
  • Can you post more code? Are you sure that's the exact response you're getting? – cookie monster Commented Mar 5, 2014 at 18:48
  • 1 Can you post the entire Buffer output. Right now it trails with ... – cookie monster Commented Mar 5, 2014 at 19:15
 |  Show 8 more comments

2 Answers 2

Reset to default 11

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 = /\0/g;
str = data.toString().replace(re, "");
var o = JSON.parse(str);
console.log(o);

or

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

or work directly on bytes in Buffer

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

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.

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

output -

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论