2014-12-08 188 views
-1

因此,该错误是太明显了,我得到了很多结果相同,但但是,没有我发现修复工作似乎正确..我只是调用一个JSON API导致“字符串”响应仅..语法错误:意外的标记JSON:解析错误

然而,当我通过它通过JSON.parse它抛出意外的标记错误..下面是示例API代码,

{ 
    states: [ 
    { 
     name: "Gujarat", 
     cities: [ ] 
    }, 
    { 
     name: "Haryana", 
     cities: [ 
     { 
      name: "Faridabad", 
      url: "faridabad", 
      popular: false 
     }, 
     { 
      name: "Gurgaon", 
      url: "gurgaon", 
      popular: false 
     } 
     ] 
    } 
    ] 
} 

,这是CoffeeScript的代码我试图执行,

request = require 'request' 

url = "http://example.com/example.v1.json" 

request url: url, json: true, (error, response, body) -> 
    if !error && response.statusCode == 200 
    jsonObject = JSON.parse body 
    console.log typeof(body) 

和JS版本

// Generated by CoffeeScript 1.8.0 
(function() { 
    var request, url; 

    request = require('request'); 

    url = "http://example.com/example.v1.json"; 

    request({ 
    url: url, 
    json: true 
    }, function(error, response, body) { 
    var jsonObject; 
    if (!error && response.statusCode === 200) { 
     jsonObject = JSON.parse(body); 
     return console.log(typeof body); 
    } 
    }); 

}).call(this); 

我得到的错误是,

SyntaxError: Unexpected token 
    at Object.parse (native) 
    at Request._callback (/Users/avinoth/learnrepo/Reponame/newtest.js:15:25) 
    at Request.self.callback (/Users/avinoth/learnrepo/Reponame/node_modules/request/request.js:121:22) 
    at Request.emit (events.js:98:17) 
    at Request.<anonymous> (/Users/avinoth/learnrepo/Reponame/node_modules/request/request.js:978:14) 
    at Request.emit (events.js:117:20) 
    at IncomingMessage.<anonymous> (/Users/avinoth/learnrepo/Reponame/node_modules/request/request.js:929:12) 
    at IncomingMessage.emit (events.js:117:20) 
    at _stream_readable.js:943:16 
    at process._tickCallback (node.js:419:13) 

传递的数据仅仅是一个字符串类型..只有这个API

而且不,我甚至尝试不同的API像https://osrc.dfm.io/avinoth.json

JSON被成功解析没有任何错误使用此链接,http://json.parser.online.fr/

好心帮我..

{ 'content-type': 'text/html; charset=UTF-8', 
    'content-length': '1436', 
    connection: 'keep-alive', 
    date: 'Mon, 08 Dec 2014 10:27:34 GMT', 
    'cache-control': 'public, max-age=5, s-maxage=5', 
    'content-encoding': 'gzip', 
    'last-modified': 'Mon, 08 Dec 2014 10:26:36 GMT', 
    etag: '"ed710ee54481a6d6e30da823bd19a511"', 
    'accept-ranges': 'bytes', 
    server: 'AmazonS3', 
    'x-cache': 'RefreshHit from cloudfront', 
    via: '1.1 05b6ee20d35c57dfb14b821a9c7edd15.cloudfront.net (CloudFront)', 
    'x-amz-cf-id': 'ZuUcdEqp-ip1GY51NX9A8x-5lwBpaEo9uX1gQK7N5gWn_mMmAvo3og==' } 

和刚才我注意到,响应主体仅仅是一个无法识别的字符一滴......

'\u001f�\b\u0000�|�T\u0000\u0003��Qo�6\fǿJ��\u0006}���6l���b+6`�=бf+�dC�\\$E���8�(5�\u001e\u0002��g[2ER|�M\u001e��v����,\u0018�{�}g���ݳ�ZM��~w�^\u0017�\u0018�a\f\u000e\f\u000e\u0007ף\u0004T\u001a�1��v��B?��{ro�`��W\t�\u0012\u000b~j��\u0019�\u0013+*��PU�׉�6�|n��\u000e\u001a�\u0012v�4\u0001�=N�\u000fN��\u0013���\u001bX��\u000fS��T\u0014a�\f��`"\n�P\u0017+|�\u0012\u000b���\n�QW���G8�W\\�H4I��a��D\\-���S��ND\u0012D(�\f��\u0002���+K�(��O��a$�i��B?�m���>5�:������\u0017贅:�l�\u0004\u0001�t\u0015��RIᱡ�.�m&\u000f\u0004g�!}�.�,�+t]��=�X�+\u001cZ��W3I\u0010!�\u001a#xK\u001eh\nU��*TPAMX"\t��\u0001C\u0006}ۤ��\u0013^t"�o�- \u000e&�\r4n�\\��\t�S�$�P��6�xT���R\u000e����pֳRurKK\u0014\u0016��6\u0003IV�f��3�\u0019�Γ㘋\u0002|�(��7ܽ\u0004��-��ޯC��0&\u0014\tz�\u0012\u000b�\u000eGhM�\\�­�4\u0016�\u0003�W���S.���\u0018�p,��D%\u0016|�����7�\u0007�\u000b�a\u0004I�\b�4��$�П��\u001f��\u0006ݙJ\u0002hC7،K��MеPl��P\u0005���\u0018k��:�b\u0001�r����<��b��0Se�BE�x[�BV��rѰRE[(/jhHm�Z|:\u000eU������\b�3|=Mt"f3��Qn�\u000bn�Ҹ3�Q��\u0006I\u0010�h�Y\u0011d ӄ����A�eO��v�,����2K�\u0002"\te\u0001^u"o�%�_��I����\u0014\u0000\f�\u001e+�sK�f.\n��\u000e沐$�\u0010I��\'����H\u0014\u001e�5�)\u0014m>\u0014�K�����-B��5���%��\u0007��%��\u000b\u0018��=�%a��c\\�,�\u0003Q�c�³\r=�l6���\u0007m*�4�\u001f��>��\u0016�X�b5�\u0004�G\u0007\u0000}蔽{�Ye>���c\u001c�o�5�oC�qi&\u0015r���2��Rt&�I������]�\t9�\'Ǎ�j\t�\t��S ~E\u0014\u0001S���WKL�8e�ƪ<���x\u000b,\u001agU�I�뇱�:�ͬ��\u001e���5B��<����\u0001$�O\u0002�\fCz\u000f�L�\u0017�e2�|ѪL\u0013�/����D�L\u0013:0��l�6S8�_�\u000e\u001a�\u0016�\u0016M��)\u001d����SK%�\u00168c$�_�%��]J���$\u001e<ժx\u001eQ����r�jSTy�P�VB]��D\u0011��kr�<F[@����x!\u0014��n�`\t��b�^� �]:�H��6����$�����E�GZ�<M\u001cі�(�9��%5\u000b�\u000fI��!\n�)���&\t<��\t�G\u0013�.3�&���&۝6\t<���\r��\u0014�3z\u0000�\\�(B\u001d��5K�y�������:RI��f��ԛ\u001c���\\��ꙒS�\t��7$$��3~\u001bro��ă�\u0010l\u0019��\\�a�߸/�՝r��5\u0006�l���C\u0001]9���W\u0006`kZ\u0017l�p�^::tEf���_x�rv��k\u0012X�ohU��g*}(]����a����V����y���l�!\u001b�.o�U���\u0015���b��\u0018�x��w�z��眬ϱ�q���~��B�\n=<��\u000f�\u00051E�GФ]�>\u0014��pn5�q\u0015�\u0012\u0015\u001c5���U�+��\u0011�\u0016�#��\u000f�\u0005�D�8̼�N�j̱�zm�F(5YcQ\u001f)Z�o�z2��1���?�\u001f\'Tp\u001c\u0000\u0000' 
+0

您遗漏的重要信息:服务器响应是什么?向我们展示返回的标题和正文。 – frhd 2014-12-08 10:19:38

+0

这里是响应标题,但身体似乎是一个无法识别的字符blob ... – avinoth 2014-12-08 10:29:25

+0

是服务器压缩数据,但不告诉任何人? – 2014-12-08 17:35:28

回答

1

最后,我发现它..内容是由这是在头文件中提到的“gzip的”,我也没在意注意到,直到这个时候..拉链由于压缩,服务器将派遣垃圾文字的其中JSON.parse无法理解,并纷纷抛出了意外标记错误..

我使用Node.js的zlib库解压缩...

下面是代码,我用它解压缩,

requestWithEncoding = (url, callback) -> 
    req = request.get url 
    req.on 'response', (res) -> 
    chunks = [] 
    res.on 'data', (chunk) -> 
     chunks.push chunk 

    res.on 'end', -> 
     buffer = Buffer.concat chunks 
     encoding = res.headers['content-encoding'] 
     if encoding == 'gzip' 
     zlib.gunzip buffer, (err, decoded) -> 
      callback err, decoded && decoded.toString() 
     else 
     callback null, buffer.toString() 

    req.on 'error', (err) -> 
    callback err 
0

它看起来像API服务器回应不是你的request模块可以处理的。也许将响应标题设置为application/json?或者在客户端,请求json?

+0

我尝试了两种方法,但它发送的是相同的响应..我已经将编辑添加到只是一个字符块的身体... – avinoth 2014-12-08 10:33:30

相关问题