我正在尝试将json解析为ul li。这是我的json:
var data = {"resultDescription":"SUCCESS","data":[{"orderNum":"A000","userName":"Oswaldo","value":504.74,"qty":3.0},{"orderNum":"A001","userName":"Mao","value":529.17,"qty":6.0},{"orderNum":"A002","userName":"Angeline","value":553.6,"qty":9.0},{"orderNum":"A003","userName":"Gerardo","value":578.03,"qty":12.0},{"orderNum":"A004","userName":"Nicki","value":602.46,"qty":15.0}]}
我试图用该代码解析它:
$.each(data.data, function (index, item) {
html += "<ul>";
console.log(item);
$.each(item.data, function (index1, item1) {
html += "<li>" + orderNum + "</li>";
html += "<li>" + qty + "</li>";
html += "<li>" + userName + "</li>";
html += "<li>" + value + "</li>";
});
html += "</ul>";
});
我做错了什么?
JSFIDDLE示例就在这里
答案 0 :(得分:1)
为什么还有$.each
$.each(item.data, function (index1, item1) {
见下文。 演示:
var data = {"resultDescription":"SUCCESS","data":[{"orderNum":"A000","userName":"Oswaldo","value":504.74,"qty":3.0},{"orderNum":"A001","userName":"Mao","value":529.17,"qty":6.0},{"orderNum":"A002","userName":"Angeline","value":553.6,"qty":9.0},{"orderNum":"A003","userName":"Gerardo","value":578.03,"qty":12.0},{"orderNum":"A004","userName":"Nicki","value":602.46,"qty":15.0}]}
var html = "";
$.each(data.data, function (index, item) {
html += "<ul>";
console.log(item);
//$.each(item.data, function (index1, item1) {
html += "<li>" + item.orderNum + "</li>";
html += "<li>" + item.qty + "</li>";
html += "<li>" + item.userName + "</li>";
html += "<li>" + item.value + "</li>";
//});
html += "</ul>";
});
setTimeout(function() {
$(".container").append(html);
}, 1500);
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container"></div>
答案 1 :(得分:0)
var html = "";
$.each(data.data, function (index, item) {
html += "<ul>";
console.log(item);
html += "<li>" + item.orderNum + "</li>";
html += "<li>" + item.qty + "</li>";
html += "<li>" + item.userName + "</li>";
html += "<li>" + item.value + "</li>";
html += "</ul>";
});
setTimeout(function () {
$(".container").append(html);
}, 1500);
试试这个
您已经从data
$.each(data.data, function (index, item) {
获取了数据addWidget()
,无需进行其他迭代。
答案 2 :(得分:0)
删除第二个var html = "";
$.each(data.data, function (index, item) {
console.log(data.data)
html += "<ul>";
html += "<li>" + item.orderNum + "</li>";
html += "<li>" + item.qty + "</li>";
html += "<li>" + item.userName + "</li>";
html += "<li>" + item.value + "</li>";
html += "</ul>";
});
setTimeout(function() {
$(".container").append(html);
}, 1500);
和前面的项目,如'item.ordernum'。看看这个Fiddle:
以下是更新后的代码:
item.data
我们要求您删除的原因是因为ResourceSet
不包含任何内容,您可以在数组上应用$ .each,此项也是一个对象,因此您可以直接访问它。
答案 3 :(得分:0)
以上答案是正确的。但是你可以使用jquery来构建html元素,而不是通过字符串连接来创建它。
$.each(data.data, function (index, item) {
var ul = $("<ul/>");
var li = $("<li/>", { text: item.orderNum });
ul.append(li);
ul.append(li.clone().text(item.qty));
ul.append(li.clone().text(item.userName));
ul.append(li.clone().text(item.value));
$(".container").append(ul);
});
答案 4 :(得分:0)
我发现了2个错误
1- $.each(item
应为orderNum
内循环
2-内环item1.orderNum
应为 var html = "";
$.each(data.data, function (index, item) {
html += "<ul>";
console.log(item);
$.each(item, function (index1, item1) {
html += "<li>" + item1.orderNum + "</li>";
html += "<li>" + item1.qty + "</li>";
html += "<li>" + item1.userName + "</li>";
html += "<li>" + item1.value + "</li>";
});
html += "</ul>";
});
setTimeout(function() {
$(".container").html(html);
}, 1500);
,对于其下的其他项目应为
labels[i].frame.height = self.view.frame.size.height