gpt4 book ai didi

jquery - 我认为我很愚蠢......但我无法正确获得 JSON 输出

转载 作者:行者123 更新时间:2023-12-01 01:27:15 25 4
gpt4 key购买 nike

我正在尝试构建一个购物车。

有一个非常简单的类,它创建一个 generic.dictionary(of string, generic.dictionary(of string, string) ,其中包含我的购物车商品。

初始字典的键是商品 ID,因此我可以使用 cartDictionary.ContainsKey(id) 轻松检查该商品是否已在购物车中,然后增加数量或添加新商品根据需要。

“购买”按钮会触发 AJAX 化的 Web 方法,该方法返回如下所示的数据:

{
"d": {
"7907": {
"id": "7907",
"qty": "4",
"singlePrice": "1185"
},
"2698": {
"id": "2698",
"qty": "1",
"singlePrice": "1322"
}
}
}

初始项目 d 是由 AJAX 帖子自动创建的,原因我不明白,但这并不重要,因此我的输出是 data.d 在我的AJAX成功,如下:

success: function (data) {
result = [data.d];
}

现在,我需要能够获取内部数据以将其呈现到购物车中

所以我需要能够按 ID 循环遍历项目并提取

id
qty
singlePrice

这样我就可以在浏览器中显示它,但我会斗鸡眼试图解决它。

我尝试在 data.d 周围添加 [,例如

var result = [data.d]

并尝试在结果中循环

result = [data.d];

$(result).each(function (i, thing) {
var thisOne = (result[i]);
//alert(thing); //<< returns object object
$(thisOne).each(function (j, val) {
alert(thisOne + " - " + val.id); //<< both thisOne and val.id return object object
});
});

通过警报的返回,我清除了获取某种 JSON 对象的信息,但显然我混淆了一些东西!

我不是程序员,但正在从事一个慢慢让我发疯的项目!!

最佳答案

你应该这样做

var data = {
"d": {
"7907": {
"id": "7907",
"qty": "4",
"singlePrice": "1185"
},
"2698": {
"id": "2698",
"qty": "1",
"singlePrice": "1322"
}
}
}



var result = data.d;

$.each(result , function(ind, el) {
//alert(thing); //<< returns object object
alert(ind + " - " + el.id);
});

在这里摆弄http://jsfiddle.net/VHPQX/

关于jquery - 我认为我很愚蠢......但我无法正确获得 JSON 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9813231/

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