【问题标题】:Why cannot I parse this json string in javascript?为什么我不能在 javascript 中解析这个 json 字符串?
【发布时间】:2015-03-20 20:06:21
【问题描述】:

我需要通过下面的代码将一个长字符串解析成一个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 where 解析?
  • 为什么首先在 字符串文字 中有 JSON?这是相当不寻常的。如果您从请求中获取 JSON 或者它是生成的,那么您描述的问题不存在。否则使用对象字面量来创建对象。

标签: javascript json parsing


【解决方案1】:

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

每个\n 转义序列都必须是\\n

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

【讨论】:

  • 你有博客吗..请
  • @zan 不,对不起。
【解决方案2】:

JSON 不允许在其数据中使用“真正的”换行符;它只能逃脱换行符。因此,您可以通过在换行符前添加一个额外的斜杠(“one\\ntwo”)来做到这一点。

【讨论】:

  • "JSON 不允许在其数据中使用“真正的”换行符;" 至少不能在内部字符串中,是的。但是,不需要在 JSON 中转义 \n["foo\nbar"] 是有效的 JSON。
  • 但是 V8 问题code.google.com/p/v8/issues/detail?id=616 上的评论#2 明确表示:“如果您打算将其解析为包含 \n 换行符转义的字符串,则需要在字符串文字中转义反斜杠。”并且评论#6 和 7 也支持这一点。
  • 在 JavaScript 字符串字面量 中讨论 JSON。在这种情况下,问题在于字符串文字。这不适用于 JSON 本身。
猜你喜欢
  • 2012-06-20
  • 2015-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多