我正在努力确定json中的简单问卷结构
所以测试可以有多个部分,
每个部分可以有多个子部分
每个子部分必须有一个或多个问题
我使用JSONLint检查语法,但显然我不能对同一个对象使用相同的元素(sub_section,ot question)两次。这就是我得到的:
SyntaxError: Duplicate key 'question' on line 13
这是我创建的JSON:
"test_sections": [{
"section": [{
"identifier": "section1",
"caption": "translate.section1",
"sub_section": [{
"identifier": "sub_section1",
"caption": "translate.section1.sub_section1",
"question": {
"identifier": "question1",
"caption": "translate.section1.sub_section1.question1",
"question": "How are you?"
},
"question": {
"identifier": "question1",
"caption": "translate.section1.sub_section1.question1",
"question": "How are you?"
}
}],
"sub_section": [{
"identifier": "sub_section2",
"caption": "translate.section1.sub_section2",
"question": {
"identifier": "question2",
"caption": "translate.section1.sub_section2.question2",
"question": "How is your day?"
}
}]
}],
"section": [{
"identifier": "section2",
"caption": "translate.section2",
"sub_section": [{
"identifier": "sub_section1",
"caption": "translate.section2.sub_section1",
"question": {
"identifier": "question1",
"caption": "translate.section2.sub_section1.question1",
"question": "What is the time now?"
}
}],
"sub_section": [{
"identifier": "sub_section2",
"caption": "translate.section2.sub_section2",
"question": {
"identifier": "question2",
"caption": "translate.section2.sub_section2.question2",
"question": "What is your favorite pet?"
}
}]
}]
}]
答案 0 :(得分:1)
不是重复相同的键,而是将键命名为复数,并将其作为值赋予数组:
{
"test_sections": [{
"identifier": "section1",
"caption": "translate.section1",
"sub_sections": [{
"identifier": "sub_section1",
"caption": "translate.section1.sub_section1",
"questions": [{
"identifier": "question1",
"caption": "translate.section1.sub_section1.question1",
"question": "How are you?"
}, {
"identifier": "question1",
"caption": "translate.section1.sub_section1.question1",
"question": "How are you?"
}]
}, {
"identifier": "sub_section2",
"caption": "translate.section1.sub_section2",
"questions": [{
"identifier": "question2",
"caption": "translate.section1.sub_section2.question2",
"question": "How is your day?"
}]
}]
}, {
"identifier": "section2",
"caption": "translate.section2",
"sub_sections": [{
"identifier": "sub_section1",
"caption": "translate.section2.sub_section1",
"questions": [{
"identifier": "question1",
"caption": "translate.section2.sub_section1.question1",
"question": "What is the time now?"
}]
}, {
"identifier": "sub_section2",
"caption": "translate.section2.sub_section2",
"questions": [{
"identifier": "question2",
"caption": "translate.section2.sub_section2.question2",
"question": "What is your favorite pet?"
}]
}]
}]
}