About JSON-formatted data transfer object failed

  Today encounter a JSON data transfer object fails the amount of problems, beginning service returns json is not very clean, the beginning and end of a line break, obviously not in conformity with the json format JSON.parse certainly given it

  

  Here to tell us the location of the first word json is not expected. Then the first line break is replaced with a positive

  chunk = chunk.replace(/[\r\n]/g, "")
  And then print it before and after the data observed
  Start direct access to data, you can see down there are a lot of blank lines
      

  Regular replaced over the data, we can see the data and common json format the same, but when converted to an object or an error, the error is still the beginning of the prompt Unexpected token in JSON at position 0

  

  Then again this string of separate strings out and turn in a browser in the node, and all the success! what? ? ? I met a ghost? ? ?

  Take a deep breath, calm down, that we first make a variable length print length, and will be replaced after printing directly to the length of this string compare and see  

  Our eyes have seen the length of the string is 896, but the actual length of the variable is indeed 897, see the error message prompt is in conjunction with the first character of exceptions, basically to determine the source of that we do not see that the first character, then Dir see what it is a character,

  console.log ( 'afterRepalce', chunk, chunk.length, 'chunk [0]:', chunk [0]), see the first printed character is empty, therefore the final reason is not returned data json clean, much space, coupled with the removal of spaces can be a regular replacement
  chunk = chunk.replace(/(^\s*)|(\s*$)/g, "")

  

  

  Than meets the eye, in fact, not so much strange question, careful analysis of the information currently available, the patient thought, to cross check information, after all, the truth will emerge.

 

 

 

 

Guess you like

Origin www.cnblogs.com/dfzc/p/11418207.html