javascript - JSON 属性无法访问 - “无法读取未定义的属性”
问题描述
我的json:
{
locale: "en",
title: " Survey",
focusFirstQuestionAutomatic: false,
pages: [
{
name: "livingEnvironment",
elements: [
{
type: "html",
name: "navigationWarning",
html: "warning"
},
{
type: "html",
name: "IntroEnvironment",
html: "We will now ask you questions about your living environment ."
},
{
type: "text",
name: "numhousehold",
width: "auto",
title: "How many people (including yourself) lived in your household at the time of Hurricane Harvey? ",
validators: [
{
type: "numeric",
text: "Please enter a number between 1 and 99.",
minValue: 1,
maxValue: 99
},
{
type: "expression",
text: "you wrong here",
expression: "{numhousehold} > {householdtype.children}"
}
],
inputType: "number"
},
{
type: "multipletext",
name: "householdtype",
width: "auto",
title: "Of these, how many (including yourself) were:",
items: [
{
name: "children",
inputType: "number",
title: "Children under 18 years old",
validators: [
{
type: "regex",
text: "One of the numbers below is out of range. Please enter 0, a positive number, or leave the box blank.",
regex: "^(\\s*|\\d+)$"
}
]
},
{
name: "adults",
inputType: "number",
title: "Adults between 18-59 years old",
validators: [
{
type: "regex",
text: "One of the numbers below is out of range. Please enter 0, a positive number, or leave the box blank.",
regex: "^(\\s*|\\d+)$"
}
]
},
{
name: "seniors",
inputType: "number",
title: "Seniors (60+)",
validators: [
{
type: "regex",
text: "One of the numbers below is out of range. Please enter 0, a positive number, or leave the box blank.",
regex: "^(\\s*|\\d+)$"
}]
}
]
}
]
}]
};
我的javascript是:
function serverValidateQuestion(survey, options) {
console.log('Validation called');
console.log(options);
console.log(options.data.numhousehold);
console.log(options.data.householdtype);
console.log(options.data.householdtype.children);
所以我可以访问“console.log(options.data.householdtype)”,它给了我家庭类型中的项目列表。但是,我正在尝试从家庭类型访问“儿童”值输入。当我写 console.log(options.data.householdtype.children) 时,它给了我“无法读取未定义的属性”错误
解决方案
var options = {
"locale": "en",
"title": " Survey",
"focusFirstQuestionAutomatic": false,
"pages": [{
"name": "livingEnvironment",
"elements": [{
"type": "html",
"name": "navigationWarning",
"html": "warning"
},
{
"type": "html",
"name": "IntroEnvironment",
"html": "We will now ask you questions about your living environment ."
},
{
"type": "text",
"name": "numhousehold",
"width": "auto",
"title": "How many people (including yourself) lived in your household at the time of Hurricane Harvey? ",
"validators": [{
"type": "numeric",
"text": "Please enter a number between 1 and 99.",
"minValue": 1,
"maxValue": 99
},
{
"type": "expression",
"text": "you wrong here",
"expression": "{numhousehold} > {householdtype.children}"
}
],
"inputType": "number"
},
{
"type": "multipletext",
"name": "householdtype",
"width": "auto",
"title": "Of these, how many (including yourself) were:",
"items": [{
"name": "children",
"inputType": "number",
"title": "Children under 18 years old",
"validators": [{
"type": "regex",
"text": "One of the numbers below is out of range. Please enter 0, a positive number, or leave the box blank.",
"regex": "^(\\s*|\\d+)$"
}]
},
{
"name": "adults",
"inputType": "number",
"title": "Adults between 18-59 years old",
"validators": [{
"type": "regex",
"text": "One of the numbers below is out of range. Please enter 0, a positive number, or leave the box blank.",
"regex": "^(\\s*|\\d+)$"
}]
},
{
"name": "seniors",
"inputType": "number",
"title": "Seniors (60+)",
"validators": [{
"type": "regex",
"text": "One of the numbers below is out of range. Please enter 0, a positive number, or leave the box blank.",
"regex": "^(\\s*|\\d+)$"
}]
}
]
}
]
}]
};
//var options = JSON.parse(str); //don't need if it's already JSON
console.log('Validation called');
console.log(options.pages[0].elements[3].items[0].name); //accesses elements without a loop
//loop through all the nested elements to get the names
for(var page in options.pages) {
console.log("Page name: " + options.pages[page].name);
for(var element in options.pages[page].elements) {
console.log("Element name: " + options.pages[page].elements[element].name);
for(var item in options.pages[page].elements[element].items) {
console.log("Item name: " + options.pages[page].elements[element].items[item].name);
}
}
}
这是我根据您的新问题更新的答案:
同样,“children”在“items”数组中:“householdtype”下没有数组。此外,如果您可以控制收到的 json,将其保持为上面显示的 json 格式(而不是字符串)有助于使您的代码更简单、更易于调试,并且您不必使用parse()
.
推荐阅读
- c# - 无法将 SmartCard 与 winscard.dll 连接?
- r - 在 R 中聚合每个组的唯一字符值
- javascript - 如何在一个文件中加入两个以上分离的 JS 库
- apache - SSL 在 xampp 中引发错误,即使在重新安装 Apache 时也是如此
- c++ - 使 Qmake 项目中的源代码依赖于协议缓冲区
- java - 带有 Oracle com.zaxxer.hikari.pool.HikariProxyCallableStatement 的 HikariCP 无法转换为 oracle.jdbc.OracleCallableStatement
- c# - 使用特定于环境的 appsettings.json 文件时,是否有办法按设置替换而不是添加?
- python - 如何从网页中的表格中抓取所有元素?
- opengl - 链接失败 - SDL(ld:未找到框架 SDL)
- unix - 如何实时读取目录中创建的 Unix 日志?