解析json仅部分起作用 [英] Parsing json works only partially

查看:70
本文介绍了解析json仅部分起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用Bing Search API来获取新闻搜索的json结果.我得到一个json结果.由于我不熟悉所有这些,因此我试图使JSON.parse()在Javascript中起作用.现在,它不适用于以下代码:

I have been trying to get json result for news search using the Bing Search API. I get a json result. Since I am new to all this, I am trying to just making the JSON.parse() thing work in Javascript. Now, it does not work for the following code:

<!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>

但是,它确实适用于以下代码:

However, it does work for this code:

<!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>

这两个代码之间的区别是结果数组中有一个额外的条目.

The difference between the two codes is there is an extra entry in the results array.

我的意思是不起作用是什么也没显示. 我已经使用在线json验证器检查了两个json.两者都有效.

What I mean by does not work is that nothing gets displayed. I have checked both json using online json validators. Both are valid.

如果我在结果数组中放入其他多余的条目,则可以正常工作.它仅在结果数组包含第二个条目时才起作用.如果我删除第二个条目并添加另外10个条目,那么它也可以工作.罪魁祸首似乎是第二项.但是如何,我找不到.

If I put other extra entries in the results array, it works fine. It only does not work when the result array contains the second entry. If I remove the second entry and add another 10 entries, then also it works. The culprit seems to be the second entry. But how, I can't find out.

问题不限于此. Bing Search API的其他结果也出现了类似的问题.在另一个json查询中,第五个条目似乎是问题所在.

The problem is not limited to this. Other results from the Bing Search API gave similar problems. In another json query the fifth entry seemed to be the problem.

任何人都可以告诉我我做错了什么吗?

Anybody can tell me what I am doing wrong?

推荐答案

您必须在第二个结果的Description字段中双引号,例如:

You have to double-escape the quote character in the Description field of the second result, like that:

"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 ..."

这篇关于解析json仅部分起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆