jgran jgran - 1 year ago 254
JSON Question

Get JSON page content with PhantomJS

I would like to know how to parse JSON in phantomjs. Any page content is enclosed in html (

<html><body><pre>{JSON string}</pre></body></html>
). Is there an options to remove enclosing tags or asking for a different Content-Type as "application/json"? If not, what's the best way to parse it. Is it using jQuery after including with includeJS jQuery?

Answer Source

Since you are using PhantomJS which is built of the webkit browser you have access to the native JSON library. There is no need to use page.evaluate, you can just use the plainText property on the page object.


var page = require('webpage').create();
page.open('http://somejsonpage.com', function () {
    var jsonSource = page.plainText;
    var resultObject = JSON.parse(jsonSource);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download