簡體   English   中英

為什么我不能在javascript中解析這個json字符串?

[英]Why cannot I parse this json string in javascript?

我需要通過下面的代碼將長字符串解析為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

請注意,您的字符串將在復制粘貼到驗證器時通過驗證,因為它是將\\n轉換為文字換行符的JavaScript解析器。 當您的特定JSON字符串首次被評估為JavaScript字符串文字,然后解析為JSON時,此問題才會顯現。

JSON不允許在其數據中使用“真正的”換行符; 它只能逃脫換行。 所以,你可以通過在換行符前加一個額外的斜杠(“一個\\\\ ntwo”)來做到這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM