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.
What I mean by does not work is that nothing gets displayed. I have checked both json using online json validators. Both are valid.
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.
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?