JSON Parse Unexpected token h 在字符串值中引起引号

我有一个从表中得到的 JSON 字符串,这是我的 JSON


    {"subtitle":"Information","desc":"Hi, Welcome.\\n                            <br><br>\\n                            You can access our website <a href=\\"https://test.com\\">here</a>.\\n                            <br><br>\\n                            Dont forget, cost: only $2! \\n                            <br>\\n                            <br>\\n                            <br>\\n                            <br>\\n                            Thankyou,<br>\\n                            Regards"}

当我试图做json.parse()但我得到一个错误


SyntaxError:JSON 中第 154 位的意外标记 h。


我认为这是由于"URL 或冒号中的引号引起的:。


如何将它们传递给 JSON?


更新

这是我获取数据的方式:


var body_inbox = {};

body_inbox.subtitle = 'Information';

body_inbox.desc = `Hi, Welcome.

                    <br><br>

                    You can access our website <a href="https://test.com">here</a>.

                    <br><br>

                    Dont forget, cost: only $2!

                    <br>

                    <br>

                    <br>

                    <br>

                    Thankyou,<br>

                    Regards`;


body_inbox = JSON.stringify(body_inbox);

我很困惑,我在表中发现许多数据在换行符\\n和 url中有双反斜杠<a href=\\"https://test.com\\">here</a>。我只是尝试使用 JSON.stringify 创建新数据,结果是\nand <a href=\"https://test.com\">here</a>。为什么会这样?


笔记

抱歉,前面的数据有误,有误导性。


应该<a href=\\"https://test.com\\">不是<a href="\\https://test.com\\">


皈依舞
浏览 254回答 4
4回答

慕虎7371278

双反斜杠应该是单反斜杠。一个反斜杠转义了后面的字符,所以你用双倍做的是转义第二个反斜杠。它阻塞了 href,因为后面的引号结束了字符串,之后解析器将 url 中的“h”作为原始字符命中。{ message: "...our website <a href=\\"https://test.com\\">here</a>" }//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^ parser thinks the string ends here//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;and doesn't know what to make of//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;https://...我的猜测是数据被两个不同的进程转义了两次(或者同一个进程运行了两次)。假设示例:数据被创建,并在进入数据库的过程中被转义。所以现在所有引号前面都有一个反斜杠。然后对数据进行编辑并在数据库中更新记录,然后再次运行转义。但是输入字符串从第一次开始就已经包含反斜杠,并且因为反斜杠本身很特殊并且需要转义,所以当字符串在返回数据库的途中(再次)转义时,您最终会得到双反斜杠。您可以通过在控制台中对字符串进行两次转义来看到这种情况。(这不是反斜杠,但它说明了问题):const input = '"This string is quoted."';const once = encodeURI(input);// encodes the quotes as '%22'// "%22This%20string%20is%20quoted.%22"const twice = encodeURI(once);// encodes the '%' in '%22' as '%25', and you end up with `%2522`// "%2522This%2520string%2520is%2520quoted.%2522"

一只萌萌小番薯

使用模板字符串保存数据。const&nbsp;json&nbsp;=&nbsp;`{"subtitle":"Information","desc":"Hi,&nbsp;Welcome.\\n<br><br>\\nYou&nbsp;can&nbsp;access&nbsp;our&nbsp;website&nbsp;<a&nbsp;href=\\"https://test.com\\">here</a>.\\n<br><br>\\nDont&nbsp;forget,&nbsp;cost:&nbsp;only&nbsp;$2!\\n<br>\\n<br>\\n<br>\\n<br>\\n&nbsp;Thankyou,<br>\\n&nbsp;Regards"}`&nbsp;console.log(JSON.parse(json))

白板的微信

json 中的双引号(值)只需要一个正斜杠(\)。所以你的json应该是{"subtitle":"Information","desc":"Hi,&nbsp;Welcome.\\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><br>\\n&nbsp;&nbsp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nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>\\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>\\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thankyou,<br>\\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Regards"}

沧海一幻觉

非常感谢您的所有回答。有助于找出由双反斜杠引起的解析失败,双反斜杠转义了第二个反斜杠而不是双引号"。数据有双反斜杠,因为这是来自克隆数据库的数据,我认为它发生在转换为 SQL 文件的过程中。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript