2015-05-20 2 views
1

Я пытаюсь загрузить мои данные из моего файла JSON, но, видимо, был загружен только первый атрибут. Я создал массив с помощью метода jQuery push для извлечения данных из моего файла JSON. Когда я вижу вывод массива из console.log, он выглядит отлично. only the first attribute was loaded Я не понимаю, что пошло не такУсы js не загружают представление в виде массива из json-файла

мой HTML

<h2>My message list </h2> 
{{#msg}} 
<img src= {{icon}} > 
{{subject}}    
{{detail}} 
{{date}} 
{{/msg}} 

мой скрипт

<script type="text/javascript"> 
    function loadUser1(){ 
    var template = $('#template').html(); 
    Mustache.parse(template); // optional, speeds up future uses 
     $.getJSON("messages.json", function(data) { 
      console.log('loaded'); 
      var messageslist = []; // create array here 
      $.each(data.messages, function (index, message) { 
      messageslist.push("{icon:'"+message.icon+"',subject:'"+message.subject+",detail:'"+message.detail+"',date:'"+message.date+"'}"); //push values here    
      }); 
      console.log(messageslist);//see output 
      var rendered = Mustache.render(template,{"msg":messageslist}); 
      $('#target').html(rendered); 
     }); 
    } 
    </script> 

Массив показывает в консоли: (У меня есть повторяющиеся фиктивные данные в формате JSON)

Array [ "{icon:'images/alert_bad.png',subject:'Supply Chain - Past Due,detail:'Gerry Harrison',date:'01/16/2015'}", "{icon:'images/alert_bad.png',subject:'Supply Chain - Past Due,detail:'Gerry Harrison',date:'01/16/2015'}", "{icon:'images/alert_bad.png',subject:'Supply Chain - Past Due,detail:'Gerry Harrison',date:'01/16/2015'}" ] 

ответ

2

Вы создаете объект как JSON string вместо объекта javascript, попробуйте следующее:

var rendered = Mustache.render(template,{"msg": data.messages}); 

data.messages уже корректно не нужно нажимать на другой массив.

+0

Это делает магию. Спасибо! – CherylG