質問

So here is the issue. I have a straight export from mongodb collection as json flat file. I am trying to get my phantomjs app to read and parse the flat file from MongoDB and convert it into an object for phantomjs to parse. For some reason I cannot parse the JSON string normally.

(note: NO jQuery solutions. This needs to be raw javascript)

Here is my flat file from mongodb. It seems fine:

    { "host" : "www.myfoxphilly.com", "path" : "/category/233430/entertainment", "created_at" : { "$date" : 1375199393295 }, "_id" : { "$oid" : "51f7e0a1dc12a13510000002" } }{ "host" : "www.news9.com", "path" : "/category/112032/news", "created_at" : { "$date" : 1375285798173 }, "_id" : { "$oid" : "51f9322668ee1e660c000001" } }{ "host" : "www.myfoxphilly.com", "path" : "/category/233430/entertainment", "created_at" : { "$date" : 1375285823602 }, "_id" : { "$oid" : "51f9323f68ee1e660c000002" } }

Here is the config file that is attempting to parse the above JSON flat file

var fs = require('fs');
var data = fs.read('configData.json');
var newdata =  JSON.stringify(data);
var dataobj = eval("["+newdata+"]");
console.log(typeof(newdata));

exports.tests = dataobj;

// Sample object (works fine like this):[{path:'/category/112043/sports' ,host:'www.newson6.com'}];

exports.getFileName = function(test,local) {
return 'results/' + test.host.replace(/\./g,'-').replace(/\:[0-9]+/,'').replace('-com','').replace('www-','') + test.path.replace(/\//g,'-').replace(/\?clienttype=/g, "clienttype") + ((local) ? '-locl' : '-prod')
}

So when I run phantom, I get no data. That JSON becomes one single object, instead of the object example I have in the comment section.

If I just replace the JS common library flat file import and make "data" a string, it works just fine, like so:

var data = '{"host" : "www.myfoxphilly.com", "path" : "/some/path/233409"}';

Is there some sort of issue going on with the js common library file import when I import the JSON in as a string? Help please.

ONCE AGAIN, no jQuery, I will down vote you without looking. I <3 jQuery, but you guys need to realize when it's appropriate to use (i.e. browser-based js).

役に立ちましたか?

解決

OMG! You used eval ... :P I'm suprised this question hasn't already been down voted 5 times.

On the real, excellent question.

Your problem, if @DCoder has actually posted an answer, is your JSON. A 'flat file from mongodb' is not necessarily valid JSON. Further, to make it valid you're gonna need to parse the string first:

  1. wrap it in square braces

  2. make sure you have a comma after each data line exported from mongo.

  3. Seriously, eval? Use JSON.parse twice on your converted string.

That should solve it. Everything else looks clean.

(.. eval .. I cant believe this scrub)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top