大家好,我想解析这种类型的字符串:
{"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']);
UPDATE,以未知长度for (var i=0;i<test["1"].length;i++) {
alert(test["1"][i]['OrderItemId']);
}
遍历测试这里有全自动的enojiration
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']);
}
}