为什么我不能在JavaScript中解析此json字符串?

冬冬

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

<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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我不能在javascript中向字符串对象添加属性?

为什么我不能在界面中访问此字段?

为什么我不能在Golang地图中键入字符串?

为什么我不能将Java字符串值从zscript传递到ZK中的javascript?

为什么我们不能在响应中将布尔值作为prop传递,它总是要求在我的代码中传递字符串

为什么我不能在Java中用$分割字符串

为什么我不能在awk字符串“?B”中用作分隔符?

为什么我们不能在javascript中修改字符串长度,因为我们可以修改数组的长度?

为什么我不能在RStudio中获得此字符串?

为什么我不能在C#中使用JsonConvert将Neo4jClient的查询结果(类型字符串)解析为Dictionary <string,string>

为什么不能在字符串前使用@ $前缀?

为什么Django HTML标签不能在字符串中工作?

为什么我不能在netlify中运行此脚本?

为什么我的文本文件不能在Java Eclipse中作为字符串读取?

为什么我不能在Xamarin.Forms中解析我的JSON?

为什么我不能在PHP中为类变量定义连接字符串?

为什么我不能在C ++中使用str.erase(str.begin()+ index)删除字符串中的字符?

为什么我在JavaScript多行字符串中收到“错误解析...”?

为什么我不能在Yii中更新此模型?

给定一个包含文件名的字符串,为什么我不能在Java中拆分一个字符串?

为什么\ n和<br>不能在字符串中工作?

为什么用此功能在字符串中找不到““ az”`?

为什么我不能在类定义内插字符串?

为什么我不能在给定的字符串中替换\ n?

为什么我不能在子字符串中捕获一个以上的数字?

为什么我不能更改字符串中的子字符串?

为什么我们不能在python中更改字符串的特定值

为什么我不能在字符串中使用“@”运算符?

Rust:为什么我不能在循环中匹配 mut 字符串选项?