我有来自后端的Json结构化数据,当我尝试使用Javascript JSON.parse解析它时,它为我创建了问题。我有多个具有相同密钥的节点,并且在解析时它只给出一个节点。下面是JSON结构。我也为同一个创建了jsfiddle:http://jsfiddle.net/DYZR4/,你可以复制结构并在“http://json.parser.online.fr/”中查看它,看看如何构建JS小牛肉。
{
"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":""
}
]
}
]
}
]
}
]
}
答案 0 :(得分:1)
json上不可能有“具有相同键的多个节点”,应该是一个数组。 在解析之前,您需要进行某种数据转换。
查看您的数据,“TODAY”节点是一个元素的所有数组,如果您设法将其转换为包含所有元素的数组,它应该解决您的问题。
答案 1 :(得分:1)
如果您知道每个重复键的内容,并且它们与子数据结构中使用的其他键是唯一的,则可以在解析之前使用正则表达式对其进行修改。
下面的正则表达式依赖于以下事实:为了简单起见,您的密钥将在结束引号("
)和完整冒号(:
)之间形成空格。如果不是这种情况,可以升级以处理导致问题的任何情况。
/// 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
等等,然后保存在你自己的阵列的其他地方。
如果这些键确实发生在数据集中较低的位置,您会发现它们也已被替换,但在使用数据时这将是一件容易检测和修复的事情。