递归解析 JSON

发布于 2024-10-01 02:58:02 字数 16112 浏览 7 评论 0原文

我有一个使用 Python 创建的大型 JSON 对象,现在需要在网页上显示该信息。我的问题是它的大小;里面有嵌套的数组和对象,并且在点上有几层深。我编写了一个极其不优雅(且有缺陷)的 JavaScript 函数来提取数据,但这对我来说似乎是一个递归问题,不幸的是我还不习惯这样思考。

有谁知道解析读取此类对象的好解决方案?我在这里的目标是真正动态地阅读它,因为我将不得不多次执行此操作(每次对象都会不同,但结构或多或少相同)。 目标是获取此信息并在网页上有意义地显示它。

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

编辑:这是对象。当我发布这篇文章时,我离开了我的电脑,而且它相当大。我已经删除了其中可能敏感的部分。我使用 JSON2 将其解析为字符串以外的其他内容。只是它的大小和嵌套给我带来了问题。

<代码>

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

谢谢, 泰杰

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.

Edit: 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"
        }
    ]
}

Thanks,
T.J.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

任性一次 2024-10-08 02:58:02

你的意思是这样的吗?

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 也适用


伊恩

do your mean something like this?

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);

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

cheers,
Ian

猥琐帝 2024-10-08 02:58:02

我可以理解您试图使您的问题更加具体,但根本没有足够的信息。

所呈现的 json 非常抽象,大多数键很少或根本没有推断出数据含义的上下文,结构相当广泛,使得收集含义的工作变得有问题。

“有意义”是一个模糊的、相对的和主观的术语。

请尝试压缩和澄清您的数据示例,并更具体地说明您打算如何呈现它,这样您将更有运气获得与您的要求相关的答案和指导。

干杯


这是我对最初提出的问题的原始答案

包括https://github.com/douglascrockford/JSON-js/blob/master/json2.js 在您的页面中。如果浏览器没有原生 JSON,它就会填充。

那么

var myobj = JSON.parse(myjsonstring);

自行编写只是一件愚蠢的事情。

祝你好运。

I can appreciate that you are trying to make your question more specific but there is simply not enough information.

The json presented is extremely abstract, most of the keys infer little or no context to the meaning of the data, the structure is quite expansive making the effort to glean the meaning problematic.

'Meaningful' is an ambiguous, relative and subjective term.

Please try to condense and clarify your data exemplar and be more specific in how you would propose to present it and you will have more luck getting answers and guidance relevant to your requirements.

Cheers


this is my original answer to the question as originally stated

Include https://github.com/douglascrockford/JSON-js/blob/master/json2.js in your page. If the browser does not have native JSON, it will fill in.

then

var myobj = JSON.parse(myjsonstring);

rolling your own is just a silly thing to do.

Good luck.

〃温暖了心ぐ 2024-10-08 02:58:02

我建议使用 JQuery 的 parseJSON() 函数

JQuery 的实现将使用浏览器的内置 JSON.parse() 函数(如果存在),如果不存在,它将回退到纯 JavaScript 实现。


[编辑] 正如 @Sky 所指出的,jQuery 实现比 json.org 提供的参考实现要简单得多,如果 JSON 的来源不一定受信任,这可能会出现一些问题。

我的经验是,jQuery 的解析器正确地使用任何有效的 JSON(如果有人能以其他方式展示,我非常希望看到它)。问题是,它还会使用许多不是 JSON 的东西——也就是说,它会使用(并计算)任何有效的 JavaScript 表达式。由于表达式可能包含函数调用,因此如果您的 JSON 来自不受信任的来源,则会出现安全问题。

但是,如果您的 JSON 源是可信的(例如您自己的服务器),那么我认为没有理由因更严格的实现而产生额外的开销(代码大小和性能)。如果您已经出于其他原因(您可能会或可能不会)包含 jQuery 核心,则尤其如此。

当然,所有这些都是相当迂腐的讨论,因为两种实现都会回退到内置浏览器(如果存在)——所有现代浏览器都会这样做。我的观点只是 jQuery 实现有其优点,并且在许多情况下是一个完全有效的选择。在其他情况下,来自 json.org 的参考实现可能会为您提供更好的服务

I would suggest JQuery's parseJSON() function.

JQuery's implementation will use the browser's built-in JSON.parse() function, if it exists, and if not, it'll fall back to a pure javascript implementation.


[edit] as pointed out by @Sky, the jQuery implementation is substantially simpler than the reference implementation provided by json.org, and this could present some issues in cases where the source of your JSON isn't necessarily trusted.

My experience is that jQuery's parser will correctly consume any valid JSON (if anyone can show otherwise, I would very much like to see it). The problem is that it will also consume a number of things that aren't JSON--namely, it will consume (and evaluate) any valid javascript expression. Since expressions could contain function calls, this would present a security issue if your JSON comes from an un-trusted source.

However, if the source of your JSON is trusted (eg. your own server), then I can see no reason to incur the extra overhead (both code size, and performance) of the stricter implementation. This is especially true if you're already including the jQuery core for other reasons (which you may or may not be).

Of course, all of this is a fairly pedantic discussion, since both implementations fall back to the browser built-in if it exists -- which it does in all modern browsers. My point is merely that the jQuery implementation has its strengths, and is a perfectly valid choice in many scenarios. In other situations, you may be better served by the reference implementation from json.org.

和我恋爱吧 2024-10-08 02:58:02

您可以使用 JS 模板引擎将 JSON 转换为人类可读的 HTML。

以下是使用 PURE 模板库进行递归的示例:http://beebole.com/pure/documentation/recursion-example/

这里的模板非常通用,使用 UL/LI 组合。

或者,如果您想在文本框中呈现它,您可以使用 JSON.stringify:

document.getElementById('theTextBox').value = JSON.stringify(theJson, null, 2);

上面对 stringify 的调用会将 JSON 缩进 2 个空格。

You could use a JS templating engine to transform your JSON to a humanly readable HTML.

Here is an example of recursion with the PURE templating lib: http://beebole.com/pure/documentation/recursion-example/

The template here is quite generic, using a UL/LI combination.

Or if you want to render it for instance in a textbox you can use JSON.stringify:

document.getElementById('theTextBox').value = JSON.stringify(theJson, null, 2);

The call above to stringify will indent the JSON with 2 spaces.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文