简体   繁体   中英

Why cannot I parse this json string in javascript?

I need to parse a long string into a json-object via below code:

<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;
}

However, after call the JSON.parse() function, I got an exception:

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

Who can help me to figure out how to parse this in javascript? Thank you all!

You need to escape your newlines. You've embedded literal new-line characters into your string.

Each of your \\n escape sequences needs to be \\\\n .

Note that your string will pass validation when copy-pasted into a validator because it's the JavaScript parser that turns \\n into a literal newline character. This problem only manifests itself when your particular JSON string is first evaluated as a JavaScript string literal, and then parsed as JSON.

JSON does not allow "real" newlines in its data; it can only have escaped newlines. So, You can do this by including an extra slash in front of the newline ("one\\\\ntwo").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM