2014-10-13 17 views
12

Tôi nhận unexpected token lỗi khi cố gắng phân tích chuỗi JSON nàyJSON.parse() bất ngờ lỗi thẻ

$scope.feeds = JSON.parse('[{"id":"212216417436_10152811286407437","from":{ "category":"Movie","name":"The Lord of the Rings Trilogy","id":"212216417436"},"story":"The Lord of the Rings Trilogy shared The Hobbit\'s photo.","picture":"https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xaf1/v/t1.0-9/s130x130/1912397_736719376363670_7288336626443028501_n.jpg?oh=a7e83093f61fafa7909ff84263f989e4&oe=54B4481D&__gda__=1420472696_dbd6462629a00e218c849c5ed7b49c44","link":"https://www.facebook.com/TheHobbitMovie/photos/a.291674244201521.63650.160617097307237/736719376363670/?type=1","name":"Timeline Photos","caption":"\"I have the only right.\" \n\nWhat is your favorite Thorin quote from #TheHobbit?","properties":[ { "name":"By","text":"The Hobbit","href":"https://www.facebook.com/TheHobbitMovie?ref=stream"}],"icon":"https://fbstatic-a.akamaihd.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif","actions":[ { "name":"Comment","link":"https://www.facebook.com/212216417436/posts/10152811286407437"},{ "name":"Like","link":"https://www.facebook.com/212216417436/posts/10152811286407437"}],"privacy":{ "value":""},"type":"photo","status_type":"shared_story","object_id":"736719376363670","application":{ "name":"Photos","id":"2305272732"},"created_time":"2014-10-12T21:35:41+0000","updated_time":"2014-10-12T21:37:17+0000"},{ "id":"21785951839_10152794856921840","from":{ "category":"App page","name":"9GAG","id":"21785951839"},"message":"Must NOT let my girlfriend see this!\nhttp://9gag.tv/p/a9OrlY?ref=fbl9\n\nWatch more awesome videos on your phone:\nAndroid ► http://goo.gl/Rbi9FP\niPhone/iPad ► http://goo.gl/NFMm9p","picture":"https://fbexternal-a.akamaihd.net/safe_image.php?d=AQCee84KqIBIdnIX&w=158&h=158&url=http%3A%2F%2Fd1d4324mp0stc8.cloudfront.net%2Fmedia%2Fphoto%2FpPgXmO5QJ_600w_v1.jpg","link":"http://9gag.tv/p/a9OrlY?ref=fbl9","name":"This \"Marry Me\" Lip-Sync Proposal Is So Touching And Amazing | 9GAG.tv","caption":"9gag.tv","description":"This sets the bar pretty high!","icon":"https://fbstatic-a.akamaihd.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif","actions":[ { "name":"Comment","link":"https://www.facebook.com/21785951839/posts/10152794856921840"},{ "name":"Like","link":"https://www.facebook.com/21785951839/posts/10152794856921840"}],"privacy":{ "value":""},"type":"link","status_type":"shared_story","created_time":"2014-10-12T21:15:00+0000","updated_time":"2014-10-12T21:15:46+0000","shares":{ "count":160}}]'); 

mở rộng đối tượng chuỗi:

{ 
    "id":"212216417436_10152811286407437", 
    "from":{ 
     "category":"Movie", 
     "name":"The Lord of the Rings Trilogy", 
     "id":"212216417436" 
    }, 
    "story":"The Lord of the Rings Trilogy shared The Hobbit\'s photo.", 
    "picture":"https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xaf1/v/t1.0-9/s130x130/1912397_736719376363670_7288336626443028501_n.jpg?oh=a7e83093f61fafa7909ff84263f989e4&oe=54B4481D&__gda__=1420472696_dbd6462629a00e218c849c5ed7b49c44", 
    "link":"https://www.facebook.com/TheHobbitMovie/photos/a.291674244201521.63650.160617097307237/736719376363670/?type=1", 
    "name":"Timeline Photos", 
    "caption":"\"I have the only right.\" \n\nWhat is your favorite Thorin quote from #TheHobbit?", 
    "properties":[ 
     { 
     "name":"By", 
     "text":"The Hobbit", 
     "href":"https://www.facebook.com/TheHobbitMovie?ref=stream" 
     } 
    ], 
    "icon":"https://fbstatic-a.akamaihd.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif", 
    "actions":[ 
     { 
     "name":"Comment", 
     "link":"https://www.facebook.com/212216417436/posts/10152811286407437" 
     }, 
     { 
     "name":"Like", 
     "link":"https://www.facebook.com/212216417436/posts/10152811286407437" 
     } 
    ], 
    "privacy":{ 
     "value":"" 
    }, 
    "type":"photo", 
    "status_type":"shared_story", 
    "object_id":"736719376363670", 
    "application":{ 
     "name":"Photos", 
     "id":"2305272732" 
    }, 
    "created_time":"2014-10-12T21:35:41+0000", 
    "updated_time":"2014-10-12T21:37:17+0000" 
}, 
{ 
    "id":"21785951839_10152794856921840", 
    "from":{ 
     "category":"App page", 
     "name":"9GAG", 
     "id":"21785951839" 
    }, 
    "message":"Must NOT let my girlfriend see this!\nhttp://9gag.tv/p/a9OrlY?ref=fbl9\n\nWatch more awesome videos on your phone:\nAndroid ► http://goo.gl/Rbi9FP\niPhone/iPad ► http://goo.gl/NFMm9p", 
    "picture":"https://fbexternal-a.akamaihd.net/safe_image.php?d=AQCee84KqIBIdnIX&w=158&h=158&url=http%3A%2F%2Fd1d4324mp0stc8.cloudfront.net%2Fmedia%2Fphoto%2FpPgXmO5QJ_600w_v1.jpg", 
    "link":"http://9gag.tv/p/a9OrlY?ref=fbl9", 
    "name":"This \"Marry Me\" Lip-Sync Proposal Is So Touching And Amazing | 9GAG.tv", 
    "caption":"9gag.tv", 
    "description":"This sets the bar pretty high!", 
    "icon":"https://fbstatic-a.akamaihd.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif", 
    "actions":[ 
     { 
     "name":"Comment", 
     "link":"https://www.facebook.com/21785951839/posts/10152794856921840" 
     }, 
     { 
     "name":"Like", 
     "link":"https://www.facebook.com/21785951839/posts/10152794856921840" 
     } 
    ], 
    "privacy":{ 
     "value":"" 
    }, 
    "type":"link", 
    "status_type":"shared_story", 
    "created_time":"2014-10-12T21:15:00+0000", 
    "updated_time":"2014-10-12T21:15:46+0000", 
    "shares":{ 
     "count":160 
    } 
} 
+0

lỗi đó là do cố gắng để phân tích cú pháp hợp lệ JSON, và những gì bạn đã đăng là json không hợp lệ theo [JsonLint] (http://jsonlint.com/) –

+0

oh nó là JSON hợp lệ. JsonLint nói vậy. – Kanishka

+1

Không, đó là bởi vì bạn có thể đăng nó như là một chuỗi, nhưng thực sự bạn nên gửi mảng. –

Trả lời

29

xác minh chuỗi json của bạn. Nếu nó là ok thử này:

var jsonStr="your json string"; 
var json=JSON.stringify(jsonStr); 
json=JSON.parse(json) 
+3

stringify đã làm phép thuật. thanks – Kanishka

+6

Điều đó chuyển đổi nó từ một chuỗi JSON thành một chuỗi của một chuỗi JSON và sau đó quay lại một chuỗi JSON. Điều đó sẽ không phân tích thành công JSON và tương đương với 'var json =" chuỗi json của bạn ";' – Quentin

+0

không đúng. stringify chỉ chuyển đổi dữ liệu đầu vào thành văn bản JSON và lưu trữ văn bản JSON đó trong một chuỗi. –

1

Khi bạn gõ một chuỗi trong javascript, \ được coi là ký tự thoát, trong khi bạn cần nó trong thực tế của bạn là \ và không phải là ký tự thoát.

Bạn cần phải thay đổi \" để \\"\n-\\n, trong khi vẫn giữ \' vì nó đã là một nhân vật thoát và nó sẽ được phân tích ổn

Các vấn đề liên quan