递归解析JSON [英] Recursively parsing JSON

查看:81
本文介绍了递归解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Python创建的大型JSON对象,我现在需要在网页上显示这些信息。我的问题是它的大小;内部有嵌套数组和对象,并且在点处有几层深度。我写了一个非常不优雅(和错误)的JavaScript函数来拉出数据,但这对我来说似乎是一个递归问题,不幸的是我不习惯那样思考(还)。

I have a large JSON object that I created with Python, and I'm needing to display the information on a webpage now. My problem is the size of it; there are nested arrays and objects within, and it is several layers deep at points. I wrote an extremely inelegant (and buggy) JavaScript function to pull the data out, but this seems like a recursive problem to me, and I unfortunately am not used to thinking like that (yet).

有没有人知道解析阅读这样一个对象的好方法?我的目标是在某种程度上动态地阅读它,因为我将不得不多次这样做(并且每次对象都会有所不同,但结构或多或少相同)。 目标是获取此信息并在网页上有意义地显示。

Does anyone know of a good solution to parsing reading such an object? My goal here is to really read this somewhat dynamically, since I'm going to have to do this many more times (and each time the object will be different, but with more or less the same structure). The goal is to take this information and show it meaningfully on a web page.

如果我能进一步详细说明,请告诉我。

Let me know if I can elaborate further.

编辑:这是对象。当我发布这个时,我离开了我的电脑,而且它相当大。可能敏感的部分我已退出。我正在使用JSON2将其解析为字符串以外的其他内容。这只是给我一个问题的大小和嵌套。

Here's the object. I was away from my computer when I posted this, plus it's rather large. Parts of it that might be sensitive I've pulled out. And I'm using the JSON2 to parse this into something other than a string. It's just the sized and nestiness that's giving me a problem.


{
    "Loop300": [
        {
            "Loop310": [
                {
                    "N1": {
                        "idCode": "0400", 
                        "idQual": "ZZ", 
                        "name": "REDACTED", 
                        "entIdCode": "SF"
                    }, 
                    "N3": [
                        {
                            "address1": "REDACTED", 
                            "address2": "REDACTED"
                        }
                    ], 
                    "G61": [
                        {
                            "contactFunctionCode": "CN", 
                            "commNumber": "REDACTED", 
                            "commNumQualifier": "TE", 
                            "name": "shipping"
                        }
                    ], 
                    "N4": [
                        {}
                    ]
                }
            ], 
            "L11": [], 
            "S5": {
                "stopReasonCode": "LD", 
                "stopSeqNum": "1"
            }, 
            "Loop350": [
                {
                    "LAD": [], 
                    "OID": {
                        "weight": "161", 
                        "poNum": "ASDF", 
                        "weightCode": "L", 
                        "unitMeasure": "CA", 
                        "refID": "THING", 
                        "quantity": "6"
                    }, 
                    "Loop360": [
                        {
                            "Loop365": [], 
                            "L5": {
                                "lineNum": "1", 
                                "desc": "STUFF"
                            }
                        }
                    ]
                }, 
                {
                    "LAD": [], 
                    "OID": {
                        "weight": "104", 
                        "poNum": "ZXMO", 
                        "weightCode": "L", 
                        "unitMeasure": "CA", 
                        "refID": "STUFF", 
                        "quantity": "6"
                    }, 
                    "Loop360": [
                        {
                            "Loop365": [], 
                            "L5": {
                                "lineNum": "2", 
                                "desc": "STUFF"
                            }
                        }
                    ]
                }, 
                {
                    "LAD": [], 
                    "OID": {
                        "weight": "1833", 
                        "poNum": "ASDF", 
                        "weightCode": "L", 
                        "unitMeasure": "CA", 
                        "refID": "THEBLOB", 
                        "quantity": "40"
                    }, 
                    "Loop360": [
                        {
                            "Loop365": [], 
                            "L5": {
                                "lineNum": "3", 
                                "desc": "STUFF"
                            }
                        }
                    ]
                }, 
                {
                    "LAD": [], 
                    "OID": {
                        "weight": "229", 
                        "poNum": "FDSA", 
                        "weightCode": "L", 
                        "unitMeasure": "CA", 
                        "refID": "BATMAN", 
                        "quantity": "6"
                    }, 
                    "Loop360": [
                        {
                            "Loop365": [], 
                            "L5": {
                                "lineNum": "4", 
                                "desc": "STUFF"
                            }
                        }
                    ]
                }
            ], 
            "AT5": [], 
            "G62": [
                {
                    "date": "20100817", 
                    "datequalifier": "10"
                }, 
                {
                    "date": "20100817", 
                    "datequalifier": "38"
                }
            ], 
            "NTE": [
                {
                    "refCode": "OTH", 
                    "desc": "No Touch"
                }
            ]
        }, 
        {
            "Loop310": [
                {
                    "N1": {
                        "idCode": "9998000006", 
                        "idQual": "ZZ", 
                        "name": "REDACTED", 
                        "entIdCode": "SF"
                    }, 
                    "N3": [
                        {
                            "address1": "REDACTED"
                        }
                    ], 
                    "G61": [
                        {
                            "contactFunctionCode": "CN", 
                            "commNumber": "REDACTED", 
                            "commNumQualifier": "TE", 
                            "name": "REDACTED"
                        }
                    ], 
                    "N4": [
                        {}
                    ]
                }
            ], 
            "L11": [], 
            "S5": {
                "stopReasonCode": "LD", 
                "stopSeqNum": "2"
            }, 
            "Loop350": [
                {
                    "LAD": [], 
                    "OID": {
                        "poNum": "QWERTY", 
                        "refID": "ASDF", 
                        "unitMeasure": "PL", 
                        "quantity": "1"
                    }, 
                    "Loop360": [
                        {
                            "Loop365": [], 
                            "L5": {
                                "lineNum": "1", 
                                "desc": "PORT"
                            }
                        }
                    ]
                }
            ], 
            "AT5": [], 
            "G62": [
                {
                    "date": "20100817", 
                    "datequalifier": "10"
                }, 
                {
                    "date": "20100817", 
                    "datequalifier": "38"
                }
            ], 
            "NTE": [
                {
                    "refCode": "OTH", 
                    "desc": "Driver Count Required"
                }
            ]
        }, 
        {
            "Loop310": [
                {
                    "N1": {
                        "idCode": "9998000070", 
                        "idQual": "ZZ", 
                        "name": "PLACE", 
                        "entIdCode": "ST"
                    }, 
                    "N3": [
                        {
                            "address1": "PLACE"
                        }
                    ], 
                    "G61": [
                        {
                            "contactFunctionCode": "CN", 
                            "commNumber": "XXXXXXXXXX", 
                            "commNumQualifier": "TE", 
                            "name": "X"
                        }
                    ], 
                    "N4": [
                        {}
                    ]
                }
            ], 
            "L11": [], 
            "S5": {
                "stopReasonCode": "UL", 
                "stopSeqNum": "3"
            }, 
            "Loop350": [
                {
                    "LAD": [], 
                    "OID": {
                        "poNum": "JOE", 
                        "refID": "SUPERMAN", 
                        "unitMeasure": "PL", 
                        "quantity": "1"
                    }, 
                    "Loop360": [
                        {
                            "Loop365": [], 
                            "L5": {
                                "lineNum": "1", 
                                "desc": "PORT"
                            }
                        }
                    ]
                }
            ], 
            "AT5": [], 
            "G62": [
                {
                    "date": "20100817", 
                    "datequalifier": "68"
                }, 
                {
                    "date": "20100817", 
                    "datequalifier": "54"
                }
            ], 
            "NTE": [
                {
                    "refCode": "OTH", 
                    "desc": "No Touch"
                }
            ]
        }, 
        {
            "Loop310": [
                {
                    "N1": {
                        "idCode": "0000403803", 
                        "idQual": "ZZ", 
                        "name": "REDACTED", 
                        "entIdCode": "ST"
                    }, 
                    "N3": [
                        {
                            "address1": "REDACTED"
                        }
                    ], 
                    "G61": [
                        {
                            "contactFunctionCode": "CN", 
                            "commNumber": "0000000000", 
                            "commNumQualifier": "TE", 
                            "name": "REDACTED"
                        }
                    ], 
                    "N4": [
                        {}
                    ]
                }
            ], 
            "L11": [
                {
                    "qualifier": "DO", 
                    "refID": "THETHING"
                }
            ], 
            "S5": {
                "stopReasonCode": "UL", 
                "stopSeqNum": "4"
            }, 
            "Loop350": [
                {
                    "LAD": [], 
                    "OID": {
                        "weight": "161", 
                        "poNum": "UIP", 
                        "weightCode": "L", 
                        "unitMeasure": "CA", 
                        "refID": "JACK", 
                        "quantity": "6"
                    }, 
                    "Loop360": [
                        {
                            "Loop365": [], 
                            "L5": {
                                "lineNum": "1", 
                                "desc": "STUFF"
                            }
                        }
                    ]
                }, 
                {
                    "LAD": [], 
                    "OID": {
                        "weight": "104", 
                        "poNum": "JKLM", 
                        "weightCode": "L", 
                        "unitMeasure": "CA", 
                        "refID": "SUSAN", 
                        "quantity": "6"
                    }, 
                    "Loop360": [
                        {
                            "Loop365": [], 
                            "L5": {
                                "lineNum": "2", 
                                "desc": "STUFF"
                            }
                        }
                    ]
                }, 
                {
                    "LAD": [], 
                    "OID": {
                        "weight": "1833", 
                        "poNum": "ASDF", 
                        "weightCode": "L", 
                        "unitMeasure": "CA", 
                        "refID": "JOE", 
                        "quantity": "40"
                    }, 
                    "Loop360": [
                        {
                            "Loop365": [], 
                            "L5": {
                                "lineNum": "3", 
                                "desc": "STUFF"
                            }
                        }
                    ]
                }, 
                {
                    "LAD": [], 
                    "OID": {
                        "weight": "229", 
                        "poNum": "AAAA", 
                        "weightCode": "L", 
                        "unitMeasure": "CA", 
                        "refID": "ASDF", 
                        "quantity": "6"
                    }, 
                    "Loop360": [
                        {
                            "Loop365": [], 
                            "L5": {
                                "lineNum": "4", 
                                "desc": "STUFF"
                            }
                        }
                    ]
                }
            ], 
            "AT5": [], 
            "G62": [
                {
                    "date": "20100817", 
                    "datequalifier": "68"
                }, 
                {
                    "date": "20100817", 
                    "datequalifier": "54"
                }
            ], 
            "NTE": [
                {
                    "refCode": "OTH", 
                    "desc": "Driver Assist Required"
                }
            ]
        }
    ], 
    "SE": {
        "numSegments": "70", 
        "controlNum": "0002"
    }, 
    "Loop100BT": [], 
    "L11": [
        {
            "qualifier": "VD", 
            "refID": "SALLY"
        }, 
        {
            "qualifier": "SI", 
            "refID": "MARK"
        }, 
        {
            "qualifier": "CR", 
            "refID": "JOE"
        }, 
        {
            "qualifier": "RB", 
            "refID": "USD"
        }, 
        {
            "qualifier": "TH", 
            "refID": "REDACTED"
        }
    ], 
    "PLD": [], 
    "L3": {
        "weight": "2328", 
        "advances": "0", 
        "rateQual": "FR", 
        "charge": "05", 
        "freightRate": "", 
        "weightQual": "G", 
        "quantity": "59"
    }, 
    "B2": {
        "ordernum": "12345", 
        "paymethod": "PP", 
        "scac": "XXXX"
    }, 
    "Loop100VI": [
        {
            "N1": {
                "name": "REDACTED", 
                "entIdCode": "AA"
            }, 
            "G61": [
                {
                    "contactFunctionCode": "CN", 
                    "commNumber": "REDACTED", 
                    "contactReference": "0563", 
                    "commNumQualifier": "TE", 
                    "name": "REDACTED"
                }, 
                {
                    "contactFunctionCode": "IC", 
                    "commNumber": "REDACTED", 
                    "commNumQualifier": "EM", 
                    "name": "REDACTED"
                }, 
                {
                    "contactFunctionCode": "ZZ", 
                    "name": "REDACTED"
                }
            ]
        }
    ], 
    "G62": [
        {
            "date": "20100827", 
            "timequalifier": "1", 
            "datequalifier": "64", 
            "time": "1302"
        }
    ], 
    "Loop100CB": [], 
    "NTE": [
        {
            "refCode": "ZZZ", 
            "desc": "26"
        }, 
        {
            "refCode": "OTH", 
            "desc": "No Touch"
        }, 
        {
            "refCode": "OTH", 
            "desc": "Driver Count Required"
        }, 
        {
            "refCode": "OTH", 
            "desc": "No Touch"
        }, 
        {
            "refCode": "OTH", 
            "desc": "Driver Assist Required"
        }
    ], 
    "B2A": {
        "purpose": "00"
    }, 
    "N7": [
        {
            "equipLength": "4800", 
            "equipHeight": "0", 
            "weight": "2328", 
            "equipWidth": "0", 
            "equipNum": "ZZZZ", 
            "equipType": "TV", 
            "weightQual": "G"
        }
    ]
}

谢谢,
TJ

Thanks, T.J.

推荐答案

你的意思是这样的?

function iterateAttributesAndFormHTMLLabels(o){
    var s = '';
    for(var a in o){
        if (typeof o[a] == 'object'){
            s+='<label><font color=green>'+a+':</font></label><br />';
            s+=iterateAttributesAndFormHTMLLabels(o[a]);
        }else{
            s+='<label>'+a+': <font color=blue>'+o[a]+'</font></label><br />';
        }//end if
    }//end for
    return s;
}//end function

data = {...};

var html = iterateAttributesAndFormHTMLLabels(data);

不知道它是否适用于所有浏览器,是否适用于Chrome和firefox

No idea if it works in all browsers, does in Chrome and firefox

欢呼,
Ian

cheers, Ian

这篇关于递归解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆