2017-01-17 61 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