gpt4 book ai didi

javascript - 如何使用多个相同的键但不同的值来解析这种类似 JSON 的结构?

转载 作者:行者123 更新时间:2023-11-30 17:46:27 27 4
gpt4 key购买 nike

我有来自后端的 Json 结构化数据,当我尝试使用 Javascript JSON.parse 解析它时,它给我带来了问题。我有多个具有相同 key 的节点,在解析时它只给出一个节点。下面是 JSON 结构。我还为它创建了 jsfiddle:http://jsfiddle.net/DYZR4/,你可以复制结构并在“http://json.parser.online.fr/”中检查它,看看 JS veal 是如何构造的。

{
"metadata":{
"USER_TYPE":"APPLE LEAD"
},
"TODAY":[
{
"REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V100-5293-2013-11",
"USER_ID":"100124",
"APP_ASSIGNMENT_ID":"3",
"METRIC_ID":"101652",
"PID":"139876",
"INSTANCE_ID":"",
"IS_OFFLINE":"",
"FACILITY_ID":"5293",
"FACILITY_NAME":"ABC",
"CITY":"Singapore",
"STATE_PROVINCE":"Singapore",
"COUNTRY":"Singapore",
"FACILITY_ADDRESS":"ABCSDASDSDSADD",
"FACILITY_LOCAL_NAME":"",
"FACILITY_LOCAL_ADDRESS":"",
"LONGITUDE":"",
"LATITUDE":"",
"VISIT_EDIT_FLAG":"D",
"VS_EDIT_FLAG":"D",
"VS_PID":"139870",
"VS_METRIC_ID":"101661",
"ONSITE_LEAD":"Eve SGM",
"APPLE_LEAD":"Lara SGM",
"APPLE_VERIFICATION_LEAD":"Nina SGM",
"SR_PROGRAM_MANAGER":"Bob SGM",
"START_DATE":"14-NOV-13",
"PROTOCOL_NAME":"Code of Conduct Provision - November Protocol",
"TPA_DATE":"NOV 22,2013",
"FINAL_REPORT_DEADLINE":"NOV 26,2013",
"SHOW_DATE":"NOV 14 - NOV 15,2013",
"AUDIT_COMPANIES":[
{
"AUDIT_COMPANY":"Golder",
"AUDITORS":[
{
"TPA_AUDITOR":"Jake",
"IS_LEAD":"1",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Facility"
}
]
},
{
"TPA_AUDITOR":"Jasmi",
"IS_LEAD":"0",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Facility"
}
]
}
]
}
]
}
],
"TODAY":[
{
"REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V98-5293-2013-11",
"USER_ID":"100124",
"APP_ASSIGNMENT_ID":"3",
"METRIC_ID":"101652",
"PID":"139018",
"INSTANCE_ID":"",
"IS_OFFLINE":"",
"FACILITY_ID":"5293",
"FACILITY_NAME":"XYZ",
"CITY":"Singapore",
"STATE_PROVINCE":"Singapore",
"COUNTRY":"Singapore",
"FACILITY_ADDRESS":"",
"FACILITY_LOCAL_NAME":"",
"FACILITY_LOCAL_ADDRESS":"",
"LONGITUDE":"",
"LATITUDE":"",
"VISIT_EDIT_FLAG":"D",
"VS_EDIT_FLAG":"Y",
"VS_PID":"139014",
"VS_METRIC_ID":"101661",
"ONSITE_LEAD":"Eve",
"APPLE_LEAD":"Eve",
"APPLE_VERIFICATION_LEAD":"Nin",
"SR_PROGRAM_MANAGER":"Fanky",
"START_DATE":"14-NOV-13",
"PROTOCOL_NAME":"Protocol test by sreedhar to validate CAP",
"TPA_DATE":"NOV 22,2013",
"FINAL_REPORT_DEADLINE":"NOV 26,2013",
"SHOW_DATE":"NOV 14 - NOV 15,2013",
"AUDIT_COMPANIES":[
{
"AUDIT_COMPANY":"Verite",
"AUDITORS":[
{
"TPA_AUDITOR":"Scott SGM",
"IS_LEAD":"0",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Property Protection"
}
]
},
{
"TPA_AUDITOR":"Charles SGM",
"IS_LEAD":"1",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Ethics"
}
]
}
]
}
]
}
],
"TODAY":[
{
"REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V11-5300-2013-11",
"USER_ID":"100124",
"APP_ASSIGNMENT_ID":"3",
"METRIC_ID":"101652",
"PID":"139047",
"INSTANCE_ID":"",
"IS_OFFLINE":"",
"FACILITY_ID":"5300",
"FACILITY_NAME":"CDE",
"CITY":"Downers Grove",
"STATE_PROVINCE":"Illinois",
"COUNTRY":"USA",
"FACILITY_ADDRESS":"5200 Road",
"FACILITY_LOCAL_NAME":"",
"FACILITY_LOCAL_ADDRESS":"",
"LONGITUDE":"",
"LATITUDE":"",
"VISIT_EDIT_FLAG":"D",
"VS_EDIT_FLAG":"Y",
"VS_PID":"139040",
"VS_METRIC_ID":"101661",
"ONSITE_LEAD":"Eve",
"APPLE_LEAD":"Eve",
"APPLE_VERIFICATION_LEAD":"Nina",
"SR_PROGRAM_MANAGER":"Fanky",
"START_DATE":"14-NOV-13",
"PROTOCOL_NAME":"Code of Conduct Provision - November Protocol",
"TPA_DATE":"NOV 22,2013",
"FINAL_REPORT_DEADLINE":"NOV 26,2013",
"SHOW_DATE":"NOV 14 - NOV 15,2013",
"AUDIT_COMPANIES":[
{
"AUDIT_COMPANY":"Golder",
"AUDITORS":[
{
"TPA_AUDITOR":"Jake SGM",
"IS_LEAD":"1",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Emergency Prevention, Preparedness and Response"
},
{
"ASSIGNMENTS":"Ethics"
}
]
},
{
"TPA_AUDITOR":"Jasmin SGM",
"IS_LEAD":"0",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Labor & Human Rights"
},
{
"ASSIGNMENTS":"Management Systems"
}
]
}
]
},
{
"AUDIT_COMPANY":"Verite",
"AUDITORS":[
{
"TPA_AUDITOR":"Charles SGM",
"IS_LEAD":"1",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Emergency Prevention, Preparedness and Response"
}
]
},
{
"TPA_AUDITOR":"David SGM",
"IS_LEAD":"0",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Hazardous Substance Management and Restrictions"
},
{
"ASSIGNMENTS":"Hazardous Substance Management and Restrictions 1"
}
]
}
]
}
]
}
],
"TODAY":[
{
"REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V13-4002-2013-11",
"USER_ID":"100124",
"APP_ASSIGNMENT_ID":"3",
"METRIC_ID":"101652",
"PID":"145748",
"INSTANCE_ID":"",
"IS_OFFLINE":"N",
"FACILITY_ID":"4002",
"FACILITY_NAME":"AAC",
"CITY":"Shenzhen",
"STATE_PROVINCE":"Guangdong",
"COUNTRY":"China",
"FACILITY_ADDRESS":"Dsdiang District",
"FACILITY_LOCAL_NAME":"",
"FACILITY_LOCAL_ADDRESS":"",
"LONGITUDE":"",
"LATITUDE":"",
"VISIT_EDIT_FLAG":"D",
"VS_EDIT_FLAG":"D",
"VS_PID":"145691",
"VS_METRIC_ID":"101661",
"ONSITE_LEAD":"Eve SGM",
"APPLE_LEAD":"Lara SGM",
"APPLE_VERIFICATION_LEAD":"Nina SGM",
"SR_PROGRAM_MANAGER":"Bob SGM",
"START_DATE":"18-NOV-13",
"PROTOCOL_NAME":"Test Protocol 10",
"TPA_DATE":"DEC 06,2013",
"FINAL_REPORT_DEADLINE":"DEC 10,2013",
"SHOW_DATE":"NOV 18 - NOV 30,2013",
"AUDIT_COMPANIES":[
{
"AUDIT_COMPANY":"Golder",
"AUDITORS":[
{
"TPA_AUDITOR":"Jake SGM",
"IS_LEAD":"0",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Labor & Human Rights"
},
{
"ASSIGNMENTS":"Wages & Benefits"
}
]
},
{
"TPA_AUDITOR":"Jasmin SGM",
"IS_LEAD":"0",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Antidiscrimination"
},
{
"ASSIGNMENTS":"Labor & Human Rights"
}
]
},
{
"TPA_AUDITOR":"Yasmin SGM",
"IS_LEAD":"1",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Antidiscrimination"
}
]
}
]
},
{
"AUDIT_COMPANY":"PWC",
"AUDITORS":[
{
"TPA_AUDITOR":"Leo SGM",
"IS_LEAD":"1",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Antidiscrimination"
},
{
"ASSIGNMENTS":"Labor & Human Rights"
}
]
},
{
"TPA_AUDITOR":"Maggie SGM",
"IS_LEAD":"0",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Labor & Human Rights"
},
{
"ASSIGNMENTS":"Wages & Benefits"
}
]
},
{
"TPA_AUDITOR":"Maria SGM",
"IS_LEAD":"0",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Antidiscrimination"
},
{
"ASSIGNMENTS":"Labor & Human Rights"
},
{
"ASSIGNMENTS":"Wages & Benefits"
}
]
}
]
},
{
"AUDIT_COMPANY":"Supplier Responsibility",
"AUDITORS":[
{
"TPA_AUDITOR":"Gia SGM",
"IS_LEAD":"1",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Antidiscrimination"
},
{
"ASSIGNMENTS":"Labor & Human Rights"
},
{
"ASSIGNMENTS":"Wages & Benefits"
}
]
},
{
"TPA_AUDITOR":"Ruth SGM",
"IS_LEAD":"0",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Antidiscrimination"
},
{
"ASSIGNMENTS":"Labor & Human Rights"
}
]
},
{
"TPA_AUDITOR":"Ryan SGM",
"IS_LEAD":"0",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Labor & Human Rights"
},
{
"ASSIGNMENTS":"Wages & Benefits"
}
]
}
]
},
{
"AUDIT_COMPANY":"Verite",
"AUDITORS":[
{
"TPA_AUDITOR":"Charles SGM",
"IS_LEAD":"1",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Antidiscrimination"
},
{
"ASSIGNMENTS":"Labor & Human Rights"
}
]
},
{
"TPA_AUDITOR":"David SGM",
"IS_LEAD":"0",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Antidiscrimination"
},
{
"ASSIGNMENTS":"Labor & Human Rights"
},
{
"ASSIGNMENTS":"Wages & Benefits"
}
]
},
{
"TPA_AUDITOR":"Scott SGM",
"IS_LEAD":"0",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Labor & Human Rights"
},
{
"ASSIGNMENTS":"Wages & Benefits"
}
]
}
]
}
]
}
],
"TODAY":[
{
"REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V15-5343-2013-11",
"USER_ID":"100124",
"APP_ASSIGNMENT_ID":"3",
"METRIC_ID":"101652",
"PID":"138985",
"INSTANCE_ID":"",
"IS_OFFLINE":"",
"FACILITY_ID":"5343",
"FACILITY_NAME":"Cable",
"CITY":"Shenzhen",
"STATE_PROVINCE":"Guangdong",
"COUNTRY":"China",
"FACILITY_ADDRESS":"No. 1173 Henggang",
"FACILITY_LOCAL_NAME":"",
"FACILITY_LOCAL_ADDRESS":"",
"LONGITUDE":"",
"LATITUDE":"",
"VISIT_EDIT_FLAG":"D",
"VS_EDIT_FLAG":"D",
"VS_PID":"138969",
"VS_METRIC_ID":"101661",
"ONSITE_LEAD":"Eve SGM",
"APPLE_LEAD":"S48 Rao",
"APPLE_VERIFICATION_LEAD":"Nina SGM",
"SR_PROGRAM_MANAGER":"Fanky SGM",
"START_DATE":"14-NOV-13",
"PROTOCOL_NAME":"Protocol by sreedhar test",
"TPA_DATE":"NOV 14,2013",
"FINAL_REPORT_DEADLINE":"NOV 15,2013",
"SHOW_DATE":"NOV 14 - NOV 14,2013",
"AUDIT_COMPANIES":[
{
"AUDIT_COMPANY":"PWC",
"AUDITORS":[
{
"TPA_AUDITOR":"Leo SGM",
"IS_LEAD":"1",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Facility"
}
]
}
]
}
]
}
],
"TODAY":[
{
"REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V8-5342-2013-11",
"USER_ID":"100124",
"APP_ASSIGNMENT_ID":"3",
"METRIC_ID":"101652",
"PID":"140688",
"INSTANCE_ID":"",
"IS_OFFLINE":"N",
"FACILITY_ID":"5342",
"FACILITY_NAME":"Vitalo Packaging Co. Ltd.",
"CITY":"Suzhou",
"STATE_PROVINCE":"Jiangsu",
"COUNTRY":"China",
"FACILITY_ADDRESS":"Suzhou",
"FACILITY_LOCAL_NAME":"",
"FACILITY_LOCAL_ADDRESS":"",
"LONGITUDE":"",
"LATITUDE":"",
"VISIT_EDIT_FLAG":"D",
"VS_EDIT_FLAG":"D",
"VS_PID":"140684",
"VS_METRIC_ID":"101661",
"ONSITE_LEAD":"Eve SGM",
"APPLE_LEAD":"Lara SGM",
"APPLE_VERIFICATION_LEAD":"Nina SGM",
"SR_PROGRAM_MANAGER":"Tom SGM",
"START_DATE":"15-NOV-13",
"PROTOCOL_NAME":"Code of Conduct Provision - November Protocol",
"TPA_DATE":"NOV 22,2013",
"FINAL_REPORT_DEADLINE":"NOV 26,2013",
"SHOW_DATE":"NOV 15 - NOV 16,2013",
"AUDIT_COMPANIES":[
{
"AUDIT_COMPANY":"Golder",
"AUDITORS":[
{
"TPA_AUDITOR":"Yasmin SGM",
"IS_LEAD":"0",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Facility"
}
]
},
{
"TPA_AUDITOR":"Jasmin SGM",
"IS_LEAD":"1",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":""
}
]
}
]
}
]
}
],
"UPCOMING":[
{
"REPORT_TYPE":"UPCOMING",
"VISIT_STATUS":"Pre-Audit - Audit Planned",
"VISIT_STATUS_ID":"1",
"VISIT_ID":"V5-5349-2013-11",
"USER_ID":"100124",
"APP_ASSIGNMENT_ID":"3",
"METRIC_ID":"101652",
"PID":"145848",
"INSTANCE_ID":"",
"IS_OFFLINE":"N",
"FACILITY_ID":"5349",
"FACILITY_NAME":"Advanced Inc.",
"CITY":"Kaohsiung",
"STATE_PROVINCE":"Kaohsiung",
"COUNTRY":"Taiwan",
"FACILITY_ADDRESS":"No.18 North6",
"FACILITY_LOCAL_NAME":"",
"FACILITY_LOCAL_ADDRESS":"",
"LONGITUDE":"",
"LATITUDE":"",
"VISIT_EDIT_FLAG":"D",
"VS_EDIT_FLAG":"Y",
"VS_PID":"145840",
"VS_METRIC_ID":"101661",
"ONSITE_LEAD":"Eve SGM",
"APPLE_LEAD":"Eve SGM",
"APPLE_VERIFICATION_LEAD":"Nina SGM",
"SR_PROGRAM_MANAGER":"Bob SGM",
"START_DATE":"20-NOV-13",
"PROTOCOL_NAME":"Test Protocol 10",
"TPA_DATE":"NOV 29,2013",
"FINAL_REPORT_DEADLINE":"DEC 03,2013",
"SHOW_DATE":"NOV 20 - NOV 22,2013",
"AUDIT_COMPANIES":[
{
"AUDIT_COMPANY":"Golder",
"AUDITORS":[
{
"TPA_AUDITOR":"Jasmin SGM",
"IS_LEAD":"1",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Labor & Human Rights"
}
]
},
{
"TPA_AUDITOR":"Jake SGM",
"IS_LEAD":"0",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Wages & Benefits"
},
{
"ASSIGNMENTS":"Antidiscrimination"
}
]
}
]
}
]
}
],
"UPCOMING":[
{
"REPORT_TYPE":"UPCOMING",
"VISIT_STATUS":"Pre-Audit - Audit Planned",
"VISIT_STATUS_ID":"1",
"VISIT_ID":"V280-5287-2013-11",
"USER_ID":"100124",
"APP_ASSIGNMENT_ID":"3",
"METRIC_ID":"101652",
"PID":"139688",
"INSTANCE_ID":"",
"IS_OFFLINE":"",
"FACILITY_ID":"5287",
"FACILITY_NAME":"Uniparts",
"CITY":"Lutterworth",
"STATE_PROVINCE":"Lutterworth",
"COUNTRY":"United Kingdom",
"FACILITY_ADDRESS":"Magna Park",
"FACILITY_LOCAL_NAME":"",
"FACILITY_LOCAL_ADDRESS":"",
"LONGITUDE":"",
"LATITUDE":"",
"VISIT_EDIT_FLAG":"D",
"VS_EDIT_FLAG":"D",
"VS_PID":"139683",
"VS_METRIC_ID":"101661",
"ONSITE_LEAD":"Eve SGM",
"APPLE_LEAD":"Lara SGM",
"APPLE_VERIFICATION_LEAD":"Nina SGM",
"SR_PROGRAM_MANAGER":"Fanky SGM",
"START_DATE":"15-NOV-13",
"PROTOCOL_NAME":"Test Protocol 10",
"TPA_DATE":"NOV 22,2013",
"FINAL_REPORT_DEADLINE":"NOV 26,2013",
"SHOW_DATE":"NOV 15 - NOV 15,2013",
"AUDIT_COMPANIES":[
{
"AUDIT_COMPANY":"Golder",
"AUDITORS":[
{
"TPA_AUDITOR":"Jasmin SGM",
"IS_LEAD":"0",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":"Antidiscrimination"
}
]
},
{
"TPA_AUDITOR":"Jake SGM",
"IS_LEAD":"1",
"ASSIGNMENTS":[
{
"ASSIGNMENTS":""
}
]
}
]
}
]
}
]
}

最佳答案

一点小技巧..

如果您知道每个重复键是什么,并且它们与子数据结构中使用的其他键不同,则可以在解析之前使用正则表达式修改它们。

下面的正则表达式依赖于这样一个事实,即您的 key 将在右引号 (") 和完整冒号 (:) 之间没有空格) — 为简单起见。如果不是这种情况,则可以升级它以处理导致问题的任何情况。

 /// load the JSON string into data, shortened example used below
var data = '{ "TODAY": [], "TODAY": [], "UPCOMING": [] }',
keys = ['TODAY', 'UPCOMING'], /// set your keys here
rexp = new RegExp('"(' + keys.join('|') + ')(?=":)', 'g'),
seen = {};

data = data.replace(rexp, function(match){
if ( seen[match] ) {
return match + '_' + (seen[match]++);
}
else {
seen[match] = 1;
return match;
}
});

console.log(data);
/// { "TODAY": [], "TODAY_1": [], "UPCOMING": []}

以上内容至少可以让您从客户端获取所有数据,然后您可以单步执行每个项目并删除添加的_1_2等等,然后保存到其他地方到你自己的数组中。

如果这些键确实出现在数据集中较低的位置,您会发现它们也已被替换,但在您使用数据时这将很容易检测和修复。

关于javascript - 如何使用多个相同的键但不同的值来解析这种类似 JSON 的结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20066945/

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