I am using CasperJS in order to crawl sites. I have found an interesting thing and I am not really sure why is it happening.
So, with CasperJS (and PhantomJS) you can get into the Page context, using their Evaluate PhantomJS Evaluate CasperJS method.
I have created an object in the page context's, and in order to return it to the CasperJS I convert it to a string. Here is what I do:
casper.then(function() {
var test = this.evaluate(function() {
var test_obj = { test_obj: ['test'] }
console.log("TEST OBJECT VALUE:");
var JSON_obj = JSON.stringify(test_obj);
console.log(JSON_obj);
return JSON_obj;
});
});
So, in two different pages, the result is different:
When in this page context, the JSON_obj value is:
{"test_obj":"[\"test\"]"}
When in this other page context, the JSON_obj value is:
{"test_obj":["test"]}
This is bad because I catch the results in Ruby, and when doing JSON.parse
, for the first case it treats the array as a string... Any idea why JSON.stringify
would behave "different" when in different page contexts? Interesting to me!