简体   繁体   English

javascript:解析多级json数组

[英]javascript : parsing multi-level json array

I have a asp.net web service that returns a multi-level array. 我有一个返回多层数组的asp.net Web服务。

the json string is parse using the json2.js lib : 使用json2.js lib解析json字符串:

var 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] 唐妮[0]

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

? donnee[0].data donnee [0] .data

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

? donnee[0].data[0] donnee [0] .data [0]

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

whereas I need an array of data eg 而我需要一个数据数组,例如

? donnee[0] 唐妮[0]

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

? donnee[0].data donnee [0] .data

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

? donnee[0].data[0] 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 ? 一次解码/解析json字符串的所有级别的最佳方法是什么?

best regards 最好的祝福

I have so far not found a simple solution to decode the json string in pure array. 到目前为止,我还没有找到一种简单的解决方案来解码纯数组中的json字符串。 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);
                                    }
                                }
                            }
                        }
                    }
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM