-1

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?

grindel
  • 29
  • 1
  • 6
  • 1
    Check console for errors..Something like `Unexpected toke..` – Rayon Apr 14 '16 at 05:28
  • both jsons strings are valid? which editor you used to verify? – Umer Hayyat Apr 14 '16 at 05:30
  • As a wild guess, without testing, it's possible that putting the JSON string into your JavaScript files is causing its \u1234 unicode escape sequences to be interpreted by the JavaScript in a way that somehow produces a value that can't be literally accepted by the JSON parser. Unlikely, but maybe. Could test that by replacing all `\` inside the string with `\\` so they get through to the JSON parser in their original form. – Jeremy Apr 14 '16 at 05:31
  • http://jsonlint.com/ for verifying @UmerHayyat Edit: Also https://jsonformatter.curiousconcept.com/ and http://codebeautify.org/jsonvalidate Basically from google search – grindel Apr 14 '16 at 05:33
  • I am new to this. So, I don't know about any console. I am just saving this in a html file and using google chrome to run it. @RayonDabre – grindel Apr 14 '16 at 05:34
  • Check Gothdo's answer, it's the correct one – zerohero Apr 14 '16 at 05:35
  • It's the escaping of the double quotes at index 1370. It should be `...be ignored. \\"It will be...` instead of `\"` since in JS strings you have to escape the escaping character. This is also something to keep in mind when constructing a regexp by RegExp object. – Redu Apr 14 '16 at 05:37

1 Answers1

1

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 ..."
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
  • +1 Unexpected Token "I", the escape used in this instance is closing the json tag and making it invalid on the 2nd entry. That is why the first entry works when you remove the 2nd one. – zerohero Apr 14 '16 at 05:35
  • Yes, it works! But how do I detect these kind of errors? – grindel Apr 14 '16 at 05:39
  • As suggested in the comments.. Use `Developer tools...`.. If you are _too_ sharp, you can make out by just seeing the code ;) – Rayon Apr 14 '16 at 05:41