0

my environment is nodejs

let z = await x.text();
console.log(z)
let y = JSON.parse(z)

the console log is

{
   "errorCode":0,
   "errorMsg":"success",
   "result":{
      "msg":"\u4e70\u5bb6\u5df2",
      "uid":"104",
      "username":"\u738b\u626d",
      "userphone":"1565777778",
      "address":"\u4e0a\u6d77",
      "price":"5.00",
      "trade_state":"TRADE_SUCCESS",
      "hk_orderid":"1575524694820"
   }
}

(node:21) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token in JSON at position 0

when I copy the json string and paste it to

JSON.parse('{"errorCode":0,"errorMsg":"success","result":{"msg":"\u4e70\u5bb6\u5df2","uid":"104","username":"\u738b\u626d","userphone":"1565777778","address":"\u4e0a\u6d77","price":"5.00","trade_state":"TRADE_SUCCESS","hk_orderid":"1575524694820"}}')

inside browser console

the parsing is correct

edit: someone asks about the json, actually the json is exactly what I pasted above

{
   "errorCode":0,
   "errorMsg":"success",
   "result":{
      "msg":"\u4e70\u5bb6\u5df2",
      "uid":"104",
      "username":"\u738b\u626d",
      "userphone":"1565777778",
      "address":"\u4e0a\u6d77",
      "price":"5.00",
      "trade_state":"TRADE_SUCCESS",
      "hk_orderid":"1575524694820"
   }
}

this is also the output of console.log(z)

below are some further logs, note that only the last one has an exception

processing job id 19121211173822287103

12/12/2019 12:17:45 PM {"errorCode":0,"errorMsg":"success","result":{"msg":"\u7b49\u5f85\u8cb7\u5bb6\u4ed8\u6b3e","uid":"103","username":"\u674e\u767d","userphone":"18912345678","address":"\u7ef4\u4e5f\u7eb3\u9152\u5e97(\u77f3\u82b1\u897f\u8def\u5e97)","price":"4.00","trade_state":"WAIT_BUYER_PAY","hk_orderid":"1576120657890"}}

12/12/2019 12:17:50 PM processing job id 19121211173822287103

12/12/2019 12:17:50 PM {"errorCode":0,"errorMsg":"success","result":{"msg":"\u7b49\u5f85\u8cb7\u5bb6\u4ed8\u6b3e","uid":"103","username":"\u674e\u767d","userphone":"18912345678","address":"\u7ef4\u4e5f\u7eb3\u9152\u5e97(\u77f3\u82b1\u897f\u8def\u5e97)","price":"4.00","trade_state":"WAIT_BUYER_PAY","hk_orderid":"1576120657890"}}

12/12/2019 12:17:55 PM processing job id 19121211173822287103

12/12/2019 12:17:57 PM

12/12/2019 12:17:57 PM {"errorCode":0,"errorMsg":"success","result":{"msg":"\u4e70\u5bb6\u5df2\u652f\u4ed8","uid":"103","username":"\u674e\u767d","userphone":"18912345678","address":"\u7ef4\u4e5f\u7eb3\u9152\u5e97(\u77f3\u82b1\u897f\u8def\u5e97)","price":"4.00","trade_state":"TRADE_SUCCESS","hk_orderid":"1576120657890"}}

12/12/2019 12:17:57 PM (node:21) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token  in JSON at position 0

12/12/2019 12:17:57 PM at JSON.parse (<anonymous>)

12/12/2019 12:17:57 PM at /usr/src/app/routes/notify.js:39:26
Alexander Ho
  • 503
  • 2
  • 7
  • 18
  • Can you paste the result of `console.log(z)` – Naresh Kumar Dec 11 '19 at 17:33
  • https://stackoverflow.com/a/20392392/8813684 check if it json and then from here work backwards to the response from API if any exception is thrown. – Naresh Kumar Dec 11 '19 at 17:36
  • @NareshKumar the result of console.log(z) is pasted above: – Alexander Ho Dec 11 '19 at 17:46
  • it is valid json {"errorCode":0,"errorMsg":"success","result":{"msg":"\u4e70\u5bb6\u5df2","uid":"104","username":"\u738b\u626d","userphone":"1565777778","address":"\u4e0a\u6d77","price":"5.00","trade_state":"TRADE_SUCCESS","hk_orderid":"1575524694820"}} – Alexander Ho Dec 11 '19 at 17:47
  • 1
    Can you just show what `x` really is? And why do you use .text with await? – Naresh Kumar Dec 11 '19 at 17:52
  • @NareshKumar x is a ReadableStream, should .text is reading the readablestream, I cant show a stream here sorry – Alexander Ho Dec 11 '19 at 17:58
  • I dont think you need to do a json parse on an object. The error which you are saying comes when u try to parse an json which is already an object. Basically remove json.parse and it should be ok – Ashish Modi Dec 11 '19 at 19:31
  • However, other responses can be parsed correctly, see my edited question @AshishModi – Alexander Ho Dec 12 '19 at 14:12

1 Answers1

0

Might need to see more code, so if you are pulling the JSON from a server/url, make sure the response header is showing this as JSON datatype

curtisk
  • 19,950
  • 4
  • 55
  • 71