JSON+;jQuery-在html中并不难
我对JSON有问题, 我需要在HTML中包含JSON数据, 现在,当我查看页面上的源代码时-#内容为空-但数据正在查看, 我需要用HTML解析这些数据(几乎不需要)来处理它们 你如何解决这个问题JSON+;jQuery-在html中并不难,jquery,json,Jquery,Json,我对JSON有问题, 我需要在HTML中包含JSON数据, 现在,当我查看页面上的源代码时-#内容为空-但数据正在查看, 我需要用HTML解析这些数据(几乎不需要)来处理它们 你如何解决这个问题 $(document).ready(function () { $.getJSON('data.json', function (data) { $('#content').empty(); $.each(data, function (entryIndex, e
$(document).ready(function () {
$.getJSON('data.json', function (data) {
$('#content').empty();
$.each(data, function (entryIndex, entry) {
var html = '<div class="data" id="' + entry['number'] + '">';
html += '<b>' + entry['number'] + '</b>';
html += '<div class="product">' + entry['product'] + '</div>';
html += '<div class="price">' + entry['price'] + ' eur</div>';
html += '<section class="image"><img alt="' + entry['product'] + '" src="' + entry['image'] + '"/></section>';
if (entry['ingredients']) {
html += '<section class="ingredients">';
html += '<ul>';
$.each(entry['ingredients'], function (colorIndex, ingredients) {
html += '<li>' + ingredients + '</li>';
});
html += '</ul>';
html += '</section>';
}
$('#content').append(html);
});
});
return false;
});
$(文档).ready(函数(){
$.getJSON('data.json',函数(数据){
$('#content').empty();
$.each(数据、函数(entryIndex、条目){
var html='';
html+=''+条目['number']+'';
html+=''+条目['product']+'';
html+=''+条目['price']+'eur';
html+='';
如果(条目[“成分]){
html+='';
html+='';
$。每个(条目['Components',函数(颜色索引,成分){
html+='- '+配料+'
';
});
html+='
';
html+='';
}
$('#content').append(html);
});
});
返回false;
});
在$之外声明html
变量。each()
并将其置于$之外的行下方。each()
:
此外,如果出现条件,请在之后关闭
var html = '';
$.each(data, function(entryIndex, entry){
html += '<div class="data" id="' + entry['number'] + '">';
html += '<b>' + entry['number'] + '</b>';
html += '<div class="product">' + entry['product'] + '</div>';
html += '<div class="price">' + entry['price'] + ' eur</div>';
html += '<section class="image"><img alt="' + entry['product'] + '" src="' + entry['image'] + '"/></section>';
if(entry['ingredients']){
html += '<section class="ingredients">';
html += '<ul>';
$.each(entry['ingredients'],function(colorIndex, ingredients){
html += '<li>' + ingredients + '</li>';
});
html += '</ul>';
html += '</section>';
}
html += '</div>';
});
$('#content').append(html);
var html='';
$.each(数据、函数(entryIndex、条目){
html+='';
html+=''+条目['number']+'';
html+=''+条目['product']+'';
html+=''+条目['price']+'eur';
html+='';
如果(条目[“成分]){
html+='';
html+='';
$。每个(条目['Components',函数(颜色索引,成分){
html+='- '+配料+'
';
});
html+='
';
html+='';
}
html+='';
});
$('#content').append(html);
JSON是一个字符串。在它成为一个数组之前,您需要对其进行解析,在该数组中,您可以使用@TallmarisgetJson
解析响应,这样您就没有了toNO$.getJSON()
自动解析json并返回一个对象。因此,变量数据
是一个对象。您的
未关闭。噢,对不起,我已更正。总之,@Adrian,你试过调试数据是否为空吗?您好,tnx,但我在这个脚本中没有看到data.json文件,为什么?
var html = '';
$.each(data, function(entryIndex, entry){
html += '<div class="data" id="' + entry['number'] + '">';
html += '<b>' + entry['number'] + '</b>';
html += '<div class="product">' + entry['product'] + '</div>';
html += '<div class="price">' + entry['price'] + ' eur</div>';
html += '<section class="image"><img alt="' + entry['product'] + '" src="' + entry['image'] + '"/></section>';
if(entry['ingredients']){
html += '<section class="ingredients">';
html += '<ul>';
$.each(entry['ingredients'],function(colorIndex, ingredients){
html += '<li>' + ingredients + '</li>';
});
html += '</ul>';
html += '</section>';
}
html += '</div>';
});
$('#content').append(html);