2017-01-17 190 views
0

试图解析JSON文件,上面写着这样的: [ { “名称”:“埃里克·西” “DOB”:1994年8月29日 } ] 极品解析该文件,以便mongodb中的该字段名称使用上面的name属性自动填充。阅读JSON文件

这是我想出了:

var url = "http://theurl"; 
    if (Meteor.isServer) { 
    Meteor.methods({ 
    jsonParse: function() { 
     this.unblock(); 
     return Meteor.http.call("GET", url, data["Name"]); 
}});} 

//invoke the server method 
if (Meteor.isClient) { 
    Meteor.call("jsonParse", function(error, results) { 
    console.log(results.content); //results.data should be a JSON object 
});} 
+0

您可以使用此模块[jsonfile(https://www.npmjs.com/package/jsonfile)来解析JSON文件 – Khang

回答

1

请重写代码

if (Meteor.isClient) { 
Meteor.call("jsonParse", function(error, results) { 
let result = JSON.parse(results.content); //results.data should be a JSON object 
console.log(result); 
}); 
} 

你应该得到结果。您可以访问result.Nameresult.DOB