解析 json 仅部分起作用
Parsing json works only partially
我一直在尝试使用 Bing 搜索 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 搜索 API 的其他结果给出了类似的问题。在另一个 json 查询中,第五个条目似乎是问题所在。
谁能告诉我我做错了什么?
您必须对第二个结果的 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 ..."
我一直在尝试使用 Bing 搜索 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 搜索 API 的其他结果给出了类似的问题。在另一个 json 查询中,第五个条目似乎是问题所在。
谁能告诉我我做错了什么?
您必须对第二个结果的 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 ..."