简体   繁体   English

使用Javascript将对象从JSON文件打印到控制台

[英]Printing objects from a JSON file to the console in Javascript

I have got a bit of a basic question, but since I'm still a student I can't seem to figure it out. 我有一个基本的问题,但是由于我还是一个学生,所以我似乎无法弄清楚。

I have got the following method: 我有以下方法:

getMenus: function () {
   var self=this;
   $.getJSON('../data/voordeelmenus.json',function(data){
      self.menus=$.map(data, function(item, i){
         return new Menu(item.id,item.naam,item.type,item.omschrijving,item.samenstelling,item.prijs);    
     });
      self.menusToHtml(); 
   });

The method should (if my code is correct) get menus from a JSON file and make objects of them using the Menu constructor. 该方法(如果我的代码正确)应该从JSON文件获取菜单,并使用Menu构造函数为其创建对象。 The objects get stored in the menus array. 对象将存储在菜单数组中。 As you can see I then call to menusToHtml method, but I actually want to test if the objects got stored in the array by printing them out in the console first. 如您所见,我随后调用menusToHtml方法,但实际上我想通过首先在控制台中将它们打印出来来测试对象是否存储在数组中。 I have tried doing this by writing console.log(self.menus) but it doesn't seem to work. 我已经尝试通过编写console.log(self.menus)来做到这一点,但它似乎没有用。

Try JSON.stringify on the object first. 首先在对象上尝试JSON.stringify。 That should work. 那应该工作。

console.log(JSON.stringify(self.menus));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM