gpt4 book ai didi

javascript - 如何使用javascript或jquery解析这种类型的字符串?

转载 作者:行者123 更新时间:2023-12-01 02:12:46 26 4
gpt4 key购买 nike

嘿大家我想解析这种类型的字符串:

    {"1":
[
{"OrderItemId":832,"ItemId":995,"Price":0},
{"OrderItemId":833,"ItemId":997,"Price":0},
{"OrderItemId":834,"ItemId":999,"Price":0}
],
"2":
[
{"OrderItemId":835,"ItemId":1008,"Price":0},
{"OrderItemId":836,"ItemId":1057,"Price":0}
]
}

这对我来说非常重要,所以请大家帮助我。

最佳答案

像这样,如果你想访问OrderItemId

var test =  {"1":
[
{"OrderItemId":832,"ItemId":995,"Price":0},
{"OrderItemId":833,"ItemId":997,"Price":0},
{"OrderItemId":834,"ItemId":999,"Price":0}
],
"2":
[
{"OrderItemId":835,"ItemId":1008,"Price":0},
{"OrderItemId":836,"ItemId":1057,"Price":0}
]
}

alert(test["1"][0]['OrderItemId']);
alert(test["1"][1]['OrderItemId']);
alert(test["1"][2]['OrderItemId']);

alert(test["2"][0]['OrderItemId']);
alert(test["2"][1]['OrderItemId']);

更新,以未知长度迭代波谷测试

for (var i=0;i<test["1"].length;i++) {
alert(test["1"][i]['OrderItemId']);
}

这里有全自动枚举

var number, count = 0;
var en = [];
for(number in test) {
en[count]=number;
count++;
}
for (var r=0;r<en.length;r++) {
alert(en[r]);
for (var i=0;i<test[en[r]].length;i++) {
alert(test[en[r]][i]['OrderItemId']);
}
}

关于javascript - 如何使用javascript或jquery解析这种类型的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13028873/

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