我一直在尝试使用Bing Search API获取新闻搜索的json结果。我得到了一个json结果。由于我是这一切的新手,我试图让JSON.parse()的东西在Javascript中工作。现在,它不适用于以下代码:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var json = '{"d":{"results":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/News?Query=\u0027britain\u0027&$skip=0&$top=1","type":"NewsResult"},"ID":"361408f1-9315-432c-925c-c8f6343a14f2","Title":"Britain\u0027s royal couple visits villages around Kaziranga in Assam","Url":"http://timesofindia.indiatimes.com/india/Britains-royal-couple-visits-villages-around-Kaziranga-in-Assam/articleshow/51811196.cms","Source":"Times of India","Description":"KAZIRANGA: After a jeep safari inside the Kaziranga National Park, Duke and Duchess of Cambridge Prince William and Kate Middleton visited villages around the famed park, the Kaziranga Discovery Centre and Centre for Wildlife Rehabilitation and ...","Date":"2016-04-13T17:07:46Z"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/News?Query=\u0027\u0027&$skip=2&$top=1","type":"NewsResult"},"ID":"71cae5a9-88fd-416b-8027-08a6409cced6","Title":"Take IPL out of drought-hit Maharashtra after 30 April: Bombay HC to BCCI","Url":"http://www.firstpost.com/sports/drought-hits-ipl-bombay-hc-directs-bcci-to-move-matches-after-30-april-out-of-maharashtra-2727426.html","Source":"Firstpost","Description":"The Bombay High Court on Wednesday directed BCCI to shift all the Indian Premier League (IPL) matches after 30 April out of Maharashtra observing that the plight of drought victims cannot be ignored. \"It will be better if the IPL matches are held ...","Date":"2016-04-13T19:45:15Z"}]}}';
obj = JSON.parse(json);
document.getElementById("demo").innerHTML = obj.d.results[0].Title;
</script>
</body>
</html>
但是,它适用于此代码:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var json = '{"d":{"results":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/News?Query=\u0027britain\u0027&$skip=0&$top=1","type":"NewsResult"},"ID":"361408f1-9315-432c-925c-c8f6343a14f2","Title":"Britain\u0027s royal couple visits villages around Kaziranga in Assam","Url":"http://timesofindia.indiatimes.com/india/Britains-royal-couple-visits-villages-around-Kaziranga-in-Assam/articleshow/51811196.cms","Source":"Times of India","Description":"KAZIRANGA: After a jeep safari inside the Kaziranga National Park, Duke and Duchess of Cambridge Prince William and Kate Middleton visited villages around the famed park, the Kaziranga Discovery Centre and Centre for Wildlife Rehabilitation and ...","Date":"2016-04-13T17:07:46Z"}]}}';
obj = JSON.parse(json);
document.getElementById("demo").innerHTML = obj.d.results[0].Title;
</script>
</body>
</html>
两个代码之间的区别是结果数组中有一个额外的条目。
我的意思是不起作用是什么都没有显示出来。 我使用在线json验证器检查了两个json。两者都有效。
如果我在结果数组中添加其他额外条目,它可以正常工作。仅当结果数组包含第二个条目时,它才起作用。如果我删除第二个条目并添加另外10个条目,那么它也可以。罪魁祸首似乎是第二个条目。但是我怎么也找不到。
问题不仅限于此。 Bing Search API的其他结果也出现了类似的问题。在另一个json查询中,第五个条目似乎是问题所在。
任何人都可以告诉我我做错了什么?
答案 0 :(得分:1)
你必须双重转义第二个结果的Description
字段中的引号字符,如:
"Description": "The Bombay High Court on Wednesday directed BCCI to shift all the Indian Premier League (IPL) matches after 30 April out of Maharashtra observing that the plight of drought victims cannot be ignored. \\"It will be better if the IPL matches are held ..."