开发者

javascript : parsing multi-level json array

开发者 https://www.devze.com 2023-03-14 03:47 出处:网络
I have a asp.net web service that returns a multi-level array. the json string is parse using the json2.js lib :

I have a asp.net web service that returns a multi-level array.

the json string is parse using the json2.js lib :

v开发者_开发问答ar donnee = JSON.parse(msg.d);

the 1st level parsing is ok but the 2nd level array (data) remains as an array of objects

? donnee[0]

{...}
color: "#0000CD"
data: [[object Object],[object Object]]
label: "formol"
type: "traitement"

? donnee[0].data

[[object Object],[object Object]]
[0]: {...}
[1]: {...}

? donnee[0].data[0]

{...}
_l: ""
_x: 7
_y: 25

whereas I need an array of data e.g.

? donnee[0]

{...}
label: "traitement formol 2"
type: "traitement"
color: "#0000CD"
data: [7,25,,7,40,formol]

? donnee[0].data

[7,25,,7,40,formol]
[0]: [7,25,]
[1]: [7,40,formol]

? donnee[0].data[0]

[7,25,]
[0]: 7
[1]: 25
[2]: ""

what is the best way to decode/parse all the levels of the json string at once ?

best regards


I have so far not found a simple solution to decode the json string in pure array. For now I parse the string and replace directly the object by an array .

var donnee = JSON.parse(msg.d);

                for (var i in donnee) {

                    if (donnee.hasOwnProperty(i)) {

                        datas[i] = donnee[i];

                        for (var j in donnee[i]) {

                            if (donnee[i].hasOwnProperty(j)) {

                                var lev2 = donnee[i][j];

                                if (typeof lev2 == "object") {
                                    for (var k in donnee[i][j]) {
                                        var lev3 = donnee[i][j][k];
                                        datas[i].data[k] = new Array(lev3.x, lev3.y, lev3.l);
                                    }
                                }
                            }
                        }
                    }
                }
0

精彩评论

暂无评论...
验证码 换一张
取 消