gpt4 book ai didi

javascript - 从 json 到 ul li

转载 作者:可可西里 更新时间:2023-11-01 14:43:45 24 4
gpt4 key购买 nike

我正在尝试将 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例子在这里

最佳答案

为什么要多一个$.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>

http://jsfiddle.net/kishoresahas/2012qo9u/1/

关于javascript - 从 json 到 ul li,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33387447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com