简体   繁体   中英

Can not find Unexpected token when parsing a JSON string

I am going to display a received JSON (but received as string) from server in a webpage. The json seems escaped when it was being created. When I check details in console it says

    Uncaught SyntaxError: Unexpected token   in JSON at position 67
    at JSON.parse (<anonymous>)
    at displaySmsDetails (sms.html:108)
    at <anonymous>:1:1

I have checked the json in json validators but they find no invalid character.

 function displaySmsDetails(){ var temp='[{"date":"1589952101314","number":"989999920000","body":"بانك سامان\nبرداشت مبلغ 100,000 خريدکالا\nاز 873-1\nمانده 1,676\n1399/2/31\n09:51:35","type":"inbox"}]'; var list=JSON.parse(temp); $("#details").html(JSON.stringify(list)); } displaySmsDetails()
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="details"></div>

 function displaySmsDetails(){ var temp='[{"date":"1589952101314","number":"989999920000","body":"بانك سامان\nبرداشت مبلغ 100,000 خريدکالا\nاز 873-1\nمانده 1,676\n1399/2/31\n09:51:35","type":"inbox"}]'; var list=jQuery.parseJSON(temp.replace(/\n/g,"\\n")); console.log(list); $("#details").html(JSON.stringify(list)); } displaySmsDetails()
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <div id="details"></div>

You should escape the slash with another slash

\\n

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM