javascript - 为什么我不能在 javascript 中解析这个 json 字符串?

标签 javascript json parsing

我需要通过以下代码将长字符串解析为 json 对象:

<script type="text/javascript">
function getTestData()
{
    //I have validate this json string using several tools, it's a valid json string.
    var jsonStr = '{"has_more":true,"next_offset":3,"results":[{"test_id":"3EA70EB9-12C3-466E-5E18-95057A630980","printid":null,"url":"http:\/\/muteor.testdomain.com\/art\/Smile-450664562","title":"Challenge 578","category":"Scraps","category_path":"scraps","is_favourited":false,"is_deleted":false,"author":{"userid":"98B7A0CE-006D-281A-CBB5-B08989184B19","username":"muteor","usericon":"http:\/\/a.testdomain.net\/avatars\/m\/u\/muteor.png?2","type":"admin"},"stats":{"comments":0,"favourites":0},"published_time":1398687231,"allows_comments":true,"excerpt":"<p>From the mists of chaos comes the legend of the frog wizard.<\/p><p>Today,\n            the DeviantArt community explores the mystery of the frog wizard. What does the frog wizard look like? <strong>Show us!<\/strong><\/p>","is_mature":true,"is_downloadable":true,"content":{"src":"http:\/\/fc09.testdomain.net\/fs70\/f\/2014\/118\/8\/2\/smile_by_muteor-d7gbb02.jpg","height":768,"width":1024,"transparency":false,"filesize":82184},"thumbs":[{"src":"http:\/\/th09.testdomain.net\/fs70\/150\/f\/2014\/118\/8\/2\/smile_by_muteor-d7gbb02.jpg","height":113,"width":150,"transparency":false},{"src":"http:\/\/th01.testdomain.net\/fs70\/200H\/f\/2014\/118\/8\/2\/smile_by_muteor-d7gbb02.jpg","height":200,"width":267,"transparency":false},{"src":"http:\/\/th06.testdomain.net\/fs70\/300W\/f\/2014\/118\/8\/2\/smile_by_muteor-d7gbb02.jpg","height":225,"width":300,"transparency":false}]},{"test_id":"3EA70EB9-12C3-466E-5E18-95057A630980","printid":null,"url":"http:\/\/muteor.testdomain.com\/art\/Test2-450661202","title":"Challenge 574","category":"Scraps","category_path":"scraps","is_favourited":false,"is_deleted":false,"author":{"userid":"98B7A0CE-006D-281A-CBB5-B08989184B19","username":"muteor","usericon":"http:\/\/a.testdomain.net\/avatars\/m\/u\/muteor.png?2","type":"admin"},"stats":{"comments":0,"favourites":0},"published_time":1398685034,"allows_comments":true,"excerpt":"<p>From the mists of chaos comes the legend of the frog wizard.<\/p><p>Today,\n            the DeviantArt community explores the mystery of the frog wizard. What does the frog wizard look like? <strong>Show us!<\/strong><\/p>","is_mature":false,"is_downloadable":true,"content":{"src":"http:\/\/fc02.testdomain.net\/fs71\/f\/2014\/118\/a\/4\/test2_by_muteor-d7gb8eq.jpg","height":768,"width":1024,"transparency":false,"filesize":68953},"thumbs":[{"src":"http:\/\/th03.testdomain.net\/fs71\/150\/f\/2014\/118\/a\/4\/test2_by_muteor-d7gb8eq.jpg","height":113,"width":150,"transparency":false},{"src":"http:\/\/th03.testdomain.net\/fs71\/200H\/f\/2014\/118\/a\/4\/test2_by_muteor-d7gb8eq.jpg","height":200,"width":267,"transparency":false},{"src":"http:\/\/th00.testdomain.net\/fs71\/300W\/f\/2014\/118\/a\/4\/test2_by_muteor-d7gb8eq.jpg","height":225,"width":300,"transparency":false}]},{"test_id":"346E0322-0ED1-6A89-DCFF-C128FCB8D394","printid":null,"url":"http:\/\/muteor.testdomain.com\/art\/Test-450664268","title":"Challenge 424","category":"Scraps","category_path":"scraps","is_favourited":false,"is_deleted":false,"author":{"userid":"98B7A0CE-006D-281A-CBB5-B08989184B19","username":"muteor","usericon":"http:\/\/a.testdomain.net\/avatars\/m\/u\/muteor.png?2","type":"admin"},"stats":{"comments":0,"favourites":0},"published_time":1398687047,"allows_comments":true,"excerpt":"<p>From the mists of chaos comes the legend of the frog wizard.<\/p><p>Today,\n            the DeviantArt community explores the mystery of the frog wizard. What does the frog wizard look like? <strong>Show us!<\/strong><\/p>","is_mature":false,"is_downloadable":true,"content":{"src":"http:\/\/fc03.testdomain.net\/fs70\/f\/2014\/118\/a\/b\/test_by_muteor-d7gbarw.jpg","height":768,"width":1024,"transparency":false,"filesize":164010},"thumbs":[{"src":"http:\/\/th03.testdomain.net\/fs70\/150\/f\/2014\/118\/a\/b\/test_by_muteor-d7gbarw.jpg","height":113,"width":150,"transparency":false},{"src":"http:\/\/th03.testdomain.net\/fs70\/200H\/f\/2014\/118\/a\/b\/test_by_muteor-d7gbarw.jpg","height":200,"width":267,"transparency":false},{"src":"http:\/\/th03.testdomain.net\/fs70\/300W\/f\/2014\/118\/a\/b\/test_by_muteor-d7gbarw.jpg","height":225,"width":300,"transparency":false}]}]}';
    var data = null;
    try
    {
        data = JSON.parse(jsonStr);
    }
    catch (e)
    {
    }
    return data;
}

然而,在调用 JSON.parse() 函数后,我得到了一个异常:

SyntaxError: Unexpected token
message: "Unexpected token ↵"
stack: (...)
get stack: function () { [native code] }
set stack: function () { [native code] }
__proto__: Error

谁能帮我弄清楚如何在 javascript 中解析它? 谢谢大家!

最佳答案

你需要转义你的换行符。您已将文字换行符嵌入到字符串中。

您的每个 \n 转义序列都需要是 \\n

请注意,当将字符串复制粘贴到验证器中时,您的字符串将通过验证,因为它是将 \n 转换为文字换行符的 JavaScript 解析器。只有当您的特定 JSON 字符串首先被评估为 JavaScript 字符串文字,然后被解析为 JSON 时,才会出现此问题。

关于javascript - 为什么我不能在 javascript 中解析这个 json 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28081877/

相关文章:

java - 如何在java Rest API中使用GET方法调用传递json数据

c++ - boost::fusion解析长字符串导致栈溢出

javascript - 在 async.waterfall 方法中构建 mongoose 回调。

javascript - IE8 中的 facebook 连接 javascript 错误

angularjs - 如何在node.js "html page"中显示json stringify数组数据?

objective-c - 使用 Cocoa NSFIleManager 忽略文件夹中的 .DS_Store 和 Icon 文件

c++ - 编译 boost spirit 解析器时出现奇怪的 static_cast 编译错误

javascript - rails 4 : select DOM element with dynamically generated id

javascript - 掉落时不工作

json - 用于中间/临时存储的 Azure cosmos db 与 blob 存储