0

I have a preblem about get json data form url

example url:

https://books.google.com/books?bibkeys=ISBN:1118691784,OCLC:879947237,LCCN:&jscmd=viewapi&callback=updateGBSCover

this url is give .txt and .txt have json data

I have no idea to get json data in .txt for show in page

Thanks for help me :)

ARR.s
  • 769
  • 4
  • 20
  • 39
  • 2
    Are you looking for `JSON.parse()` ? [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) – Ploppy Mar 19 '17 at 15:06
  • 1
    The response is not JSON. It appears to be actual javascript. You could use eval but that would be a really bad idea from a security standpoint. – DrC Mar 19 '17 at 15:07
  • @Ploppy I try `this.http.get("https://xxxx").subscribe(data => { this.itemss=JSON.parse(data['_body']); console.log(this.itemss); });` it's work I get json data but it's error `EXCEPTION: Unexpected token u in JSON at position 0` – ARR.s Mar 19 '17 at 15:39
  • The file is probably not only JSON. Maybe the JSON start after a few chars. – Ploppy Mar 19 '17 at 15:43

1 Answers1

0

The data is in JSONP format, e.g. JSON wrapped in a callback

More information here: https://developers.google.com/books/

$.ajax({
  url: "https://books.google.com/books?bibkeys=ISBN:1118691784,OCLC:879947237,LCCN:&jscmd=viewapi",
  dataType: "jsonp",
  jsonpCallback: "updateGBSCover"
});

function updateGBSCover(data) {
  // console.log(data);
  $("#result").append($('<img/>',{"src": data["OCLC:879947237"].thumbnail_url}));
}    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="result"></div>
mplungjan
  • 169,008
  • 28
  • 173
  • 236