首页 > 解决方案 > 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) 时,它给了我“无法读取未定义的属性”错误

标签: javascriptjson

解决方案


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().


推荐阅读