从没有`eval`的js解析数据

时间:2018-04-11 10:14:52

标签: javascript json

我正在使用tumblr api获取某个用户的数据,响应是js而不是json:

https://taylorswift.tumblr.com/api/read?start=0&format=json&num=1

var tumblr_api_read = {"tumblelog":{"title":"Taylor Swift","description":"","name":"taylorswift","timezone":"US\/Eastern","cname":false,"feeds":[]},"posts-start":0,"posts-total":186,"posts-type":false,"posts":[{"id":"172426031730","url":"http:\/\/taylorswift.tumblr.com\/post\/172426031730","url-with-slug":"http:\/\/taylorswift.tumblr.com\/post\/172426031730\/did-you-see-what-hayley-kiyoko-said-about-taylor","type":"answer","date-gmt":"2018-03-30 22:27:43 GMT","date":"Fri, 30 Mar 2018 18:27:43","bookmarklet":0,"mobile":0,"feed-item":"","from-feed-id":0,"unix-timestamp":1522448863,"format":"html","reblog-key":"GJl0OUt4","slug":"did-you-see-what-hayley-kiyoko-said-about-taylor","is-submission":false,"like-button":"<div class=\"like_button\" data-post-id=\"172426031730\" data-blog-name=\"taylorswift\" id=\"like_button_172426031730\"><iframe id=\"like_iframe_172426031730\" src=\"https:\/\/assets.tumblr.com\/assets\/html\/like_iframe.html?_v=fc298e85f978b8662a643fe0a6b8c638#name=taylorswift&amp;post_id=172426031730&amp;color=black&amp;rk=GJl0OUt4&amp;root_id=172423870366\" scrolling=\"no\" width=\"20\" height=\"20\" frameborder=\"0\" class=\"like_toggle\" allowTransparency=\"true\" name=\"like_iframe_172426031730\"><\/iframe><\/div>","reblog-button":"<a href=\"https:\/\/www.tumblr.com\/reblog\/172426031730\/GJl0OUt4\" class=\"reblog_button\"style=\"display: block;width:20px;height:20px;\"><svg width=\"100%\" height=\"100%\" viewBox=\"0 0 21 21\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" xmlns:xlink=\"http:\/\/www.w3.org\/1999\/xlink\" fill=\"#000\"><path d=\"M5.01092527,5.99908429 L16.0088498,5.99908429 L16.136,9.508 L20.836,4.752 L16.136,0.083 L16.1360004,3.01110845 L2.09985349,3.01110845 C1.50585349,3.01110845 0.979248041,3.44726568 0.979248041,4.45007306 L0.979248041,10.9999998 L3.98376463,8.30993634 L3.98376463,6.89801007 C3.98376463,6.20867902 4.71892527,5.99908429 5.01092527,5.99908429 Z\"><\/path><path d=\"M17.1420002,13.2800293 C17.1420002,13.5720293 17.022957,14.0490723 16.730957,14.0490723 L4.92919922,14.0490723 L4.92919922,11 L0.5,15.806 L4.92919922,20.5103758 L5.00469971,16.9990234 L18.9700928,16.9990234 C19.5640928,16.9990234 19.9453125,16.4010001 19.9453125,15.8060001 L19.9453125,9.5324707 L17.142,12.203\"><\/path><\/svg><\/a>","note-count":"24565","reblogged-from-url":"http:\/\/shaking-off-the-bad-blood.tumblr.com\/post\/172423870366\/did-you-see-what-hayley-kiyoko-said-about-taylor","reblogged-from-name":"shaking-off-the-bad-blood","reblogged-from-title":"\"Samar, right?\"","reblogged_from_avatar_url_16":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_16.png","reblogged_from_avatar_url_24":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_24.png","reblogged_from_avatar_url_30":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_30.png","reblogged_from_avatar_url_40":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_40.png","reblogged_from_avatar_url_48":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_48.png","reblogged_from_avatar_url_64":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_64.png","reblogged_from_avatar_url_96":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_96.png","reblogged_from_avatar_url_128":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_128.png","reblogged_from_avatar_url_512":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_512.png","reblogged-root-url":"http:\/\/shaking-off-the-bad-blood.tumblr.com\/post\/172423870366\/did-you-see-what-hayley-kiyoko-said-about-taylor","reblogged-root-name":"shaking-off-the-bad-blood","reblogged-root-title":"\"Samar, right?\"","reblogged_root_avatar_url_16":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_16.png","reblogged_root_avatar_url_24":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_24.png","reblogged_root_avatar_url_30":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_30.png","reblogged_root_avatar_url_40":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_40.png","reblogged_root_avatar_url_48":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_48.png","reblogged_root_avatar_url_64":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_64.png","reblogged_root_avatar_url_96":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_96.png","reblogged_root_avatar_url_128":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_128.png","reblogged_root_avatar_url_512":"https:\/\/78.media.tumblr.com\/avatar_ef3080f98ef4_512.png","tumblelog":{"title":"Taylor Swift","name":"taylorswift","cname":false,"url":"http:\/\/taylorswift.tumblr.com\/","timezone":"US\/Eastern","avatar_url_16":"https:\/\/78.media.tumblr.com\/avatar_c4f07fd82125_16.png","avatar_url_24":"https:\/\/78.media.tumblr.com\/avatar_c4f07fd82125_24.png","avatar_url_30":"https:\/\/78.media.tumblr.com\/avatar_c4f07fd82125_30.png","avatar_url_40":"https:\/\/78.media.tumblr.com\/avatar_c4f07fd82125_40.png","avatar_url_48":"https:\/\/78.media.tumblr.com\/avatar_c4f07fd82125_48.png","avatar_url_64":"https:\/\/78.media.tumblr.com\/avatar_c4f07fd82125_64.png","avatar_url_96":"https:\/\/78.media.tumblr.com\/avatar_c4f07fd82125_96.png","avatar_url_128":"https:\/\/78.media.tumblr.com\/avatar_c4f07fd82125_128.png","avatar_url_512":"https:\/\/78.media.tumblr.com\/avatar_c4f07fd82125_512.png"},"question":"Did you see what Hayley Kiyoko said about Taylor? I&rsquo;m sad :(","answer":"<p>I did! I think people are actually misinterpreting the quote. I don\u2019t know if it is being taken out of context or what, but if you read the interview itself, I don\u2019t believe she was calling Taylor unoriginal or boy crazy. She was just defending her choice for wanting to make videos with female love interests. <\/p>\n\n<p>It is actually very similar to when Taylor back in 2014\/2015 would say in interviews how she believes people are sexist when they call her out for writing about her exes but don\u2019t call out people like Ed Sheeran or Bruno Mars for writing about theirs. She wasn\u2019t calling out Ed or Bruno at the time, yet rather criticizing how the media and the public handle the situation. Hayley appears to be doing the same.<\/p>"}]};

然后我想获取tumblr_api_read的数据,而我可以使用eval执行脚本并获取返回值,但是我担心eval不安全

所以我想知道是否有其他方法来获取数据?

我尝试过js-unescapehtml-unescape数据,但都没有。

2 个答案:

答案 0 :(得分:4)

documentation,我只能找到参数debug。如果使用值1传递,则返回JSON而不使用var作为前缀:

https://taylorswift.tumblr.com/api/read/json?start=0&num=1&debug=1

如果不这样做,遗憾的是,还有很多事情要做。您应该尽快切换到最新的API。

答案 1 :(得分:0)

如果你只是想从中获取JSON,你可以这样做:

JSON.parse( response.slice( response.indexOf('{') ) );

这找到了第一个开放式支架,为你提供了一切,只是没有它之前的字符串化JSON。所以你只需要解析它。