2

嘿伙计们,我想解析这种类型的字符串:

    {"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}
    ]
}

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

4

6 回答 6

4

像这样,如果你想访问 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']);
    }
}
于 2012-10-23T10:57:18.690 回答
3

这是JSON。使用来解析它。(或您喜欢的任何其他库,可能是 jQuery)

于 2012-10-23T10:55:04.037 回答
2

//你也可以这样做:

<script>
    var employees = { "accounting" : [   // accounting is an array in employees.
                                    { "firstName" : "John",  // First element
                                      "lastName"  : "Doe",
                                      "age"       : 23 },

                                    { "firstName" : "Mary",  // Second Element
                                      "lastName"  : "Smith",
                                      "age"       : 32 }
                                  ], // End "accounting" array.                                  
                  "sales"       : [ // Sales is another array in employees.
                                    { "firstName" : "Sally", // First Element
                                      "lastName"  : "Green",
                                      "age"       : 27 },

                                    { "firstName" : "Jim",   // Second Element
                                      "lastName"  : "Galley",
                                      "age"       : 41 }
                                  ] // End "sales" Array.
                } // End Employees


    var output="<ul>";
    for (var i in employees.accounting) {
        output+="<li>" + employees.accounting[i].firstName + " " + employees.accounting[i].lastName + "--" + employees.accounting[i].age+"</li>";
    }
    output+="</ul>";

    var output1="<ul>";
    for (var j in employees.sales) {
        output1+="<li>" + employees.sales[j].firstName + " " + employees.sales[j].lastName + "--" + employees.sales[j].age+"</li>";
    }
    output+="</ul>";

    alert(output1);

</script>
于 2012-10-23T11:03:34.537 回答
0

问题可能是由于行结束。

这是一个工作[在 chrome 开发控制台中测试] 代码:

var jsonStr = '{"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}\
    ]\
}';
var obj = JSON.parse(jsonStr); // works!
于 2012-10-23T10:59:09.153 回答
0

您可以使用jQuery 来解析 JSON

也看看这个 SO 问题Parse JSON in JavaScript?

于 2012-10-23T11:01:07.160 回答
0

如果每个对象项结束,则无需在结束后添加反斜杠。只能用逗号分隔。

于 2012-10-23T11:26:19.323 回答