可以从Javascript访问JSON属性

时间:2018-08-09 16:35:20

标签: javascript json

我的json:

     {
  locale: "en",
  title: "Survey",
  focusFirstQuestionAutomatic: false,
  pages: [
   {
    name: "livingEnvironment",
    elements: [
     {
      type: "html",
      name: "navigationWarning",
      html: "To navigate "
     },
     {
      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",
      validators: [
       {
        type: "numeric",
        text: "Please enter a number between 1 and 99.",
        minValue: 1,
        maxValue: 99
       }
      ],
      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+)$"
         }
        ]
       },
       {
        name: "disabled",
        inputType: "number",
        title: "Disabled",
        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.data.householdtype);
            console.log(options.data.householdtype.children);

因此我可以访问console.log(options.data.householdtype),它为我提供了家用类型的物品清单。但是,我试图访问来自familytype的“儿童”值输入。当我编写console.log(options.data.householdtype.children)时,它给了我

`
  

无法读取未定义的属性”错误。在Google控制台模式下,   “ console.log(options.data.householdtype)”给了我这个:

`“成人     :     “ 3”     孩子们     :     “ 3”     残障人士     :     “ 3”     孕     :     “ 3”     老年人     :     “ 3”     原始     :     对象

但是我只想访问children值,而console.log(options.data.householdtype.children)不起作用。此外,console.log(options.data.numhousehold)确实为我提供了用户输入的值。同样,这些值来自用户在调查期间填写的输入框。请帮助我。

1 个答案:

答案 0 :(得分:0)

好,所以我想通了,这是因为在我的html文件console.log(options.data.householdtype)的第一页上;给出未定义。在我的html的第二页上,它是否找到了Householdtype,然后便可以访问console.log(options.data.householdtype.children);否则它将无法正常工作。