gpt4 book ai didi

javascript - 递归解析JSON

转载 作者:搜寻专家 更新时间:2023-11-01 04:47:53 26 4
gpt4 key购买 nike

我有一个用 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"
}
]
}

谢谢,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 中是否可用

干杯,伊恩

关于javascript - 递归解析JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4104321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com