嵌套在JSON中

时间:2017-02-13 03:50:38

标签: arrays json

我无法通过以下JSON进行验证。它在这里崩溃了:

"Head Coach": ["Baseball", "Basketball - Boys", "Basketball", "Volleyball - Girls", "Volleyball", "Spiritline"]

没有最后一个数组就可以了。有什么想法吗?

{
    "userprofileid": 12547,
    "email": "cusdtrojans@gmail.com",
    "first_name": "Maribel",
    "middle_name": null,
    "last_name": "Vasquez",
    "prefix": null,
    "suffix": null,
    "schools": [{
        "schoolid": 417,
        "name": "Clifton High School",
        "addr": "4523 Appaloosa St.",
        "addr2": null,
        "city": "Clifton",
        "state": "AZ",
        "zip": "85533",
        "roles": ["Athletic Director",
            "Principal Secretary",
            "Theatre",
            "Media Adviser",
            "Webmaster",
            "Head Coach": ["Baseball", "Basketball - Boys", "Basketball", "Volleyball - Girls", "Volleyball", "Spiritline"]
        ]
    }]
}

3 个答案:

答案 0 :(得分:0)

尝试以下

{“userprofileid”:12547,“email”:“cusdtrojans@gmail.com”,“first_name”:“Maribel”,“middle_name”:null,“last_name”:“Vasquez”,“prefix”:null, “后缀”:null,“学校”:[{“schoolid”:417,“name”:“Clifton High School”,“addr”:“4523 Appaloosa St.”,“addr2”:null,“city”:“克利夫顿“,”状态“:”AZ“,”zip“:”85533“,”角色“:[”运动总监“,”首席秘书“,”剧院“,”媒体顾问“,”网站管理员“,”主教练“ “,”棒球“,”篮球 - 男孩“,”篮球“,”排球 - 女孩“,”排球“,”精神线“]]}}

"Head Coach":应为"Head Coach", 这是格式化的版本。

{
   "userprofileid":12547,
   "email":"cusdtrojans@gmail.com",
   "first_name":"Maribel",
   "middle_name":null,
   "last_name":"Vasquez",
   "prefix":null,
   "suffix":null,
   "schools":[
      {
         "schoolid":417,
         "name":"Clifton High School",
         "addr":"4523 Appaloosa St.",
         "addr2":null,
         "city":"Clifton",
         "state":"AZ",
         "zip":"85533",
         "roles":[
            "Athletic Director",
            "Principal Secretary",
            "Theatre",
            "Media Adviser",
            "Webmaster",
            "Head Coach",
            [
               "Baseball",
               "Basketball - Boys",
               "Basketball",
               "Volleyball - Girls",
               "Volleyball",
               "Spiritline"
            ]
         ]
      }
   ]
}

答案 1 :(得分:0)

试试这个,这可以确保你有一个名为'主教练'列表。

{
    "userprofileid": 12547,
    "email": "cusdtrojans@gmail.com",
    "first_name": "Maribel",
    "middle_name": null,
    "last_name": "Vasquez",
    "prefix": null,
    "suffix": null,
    "schools": [{
        "schoolid": 417,
        "name": "Clifton High School",
        "addr": "4523 Appaloosa St.",
        "addr2": null,
        "city": "Clifton",
        "state": "AZ",
        "zip": "85533",
        "roles": [
            "Athletic Director",
            "Principal Secretary",
            "Theatre", "Media Adviser",
            "Webmaster", {
                "Head Coach": [
                    "Baseball",
                    "Basketball - Boys",
                    "Basketball",
                    "Volleyball - Girls",
                    "Volleyball",
                    "Spiritline"
                ]
            }
        ]
    }]
}

答案 2 :(得分:0)

显然,以下内容无效

"roles": ["Athletic Director",
    "Principal Secretary",
    "Theatre",
    "Media Adviser",
    "Webmaster",
    "Head Coach": ["Baseball", "Basketball - Boys", "Basketball", "Volleyball - Girls", "Volleyball", "Spiritline"]
]

因为roles是一个数组,但是你试图在其中加入"Head Coach":,它看起来像是一个属于对象的属性。

问题在于您的数据模型:您希望在数组中将单个角色指定为字符串,但对于Head Coach的角色,您需要其他信息,即该人员为主教练的运动。如果您只是想指定该角色而没有其他信息,我建议将roles作为对象,将角色作为属性,值为true

"roles": {
  "Athletic Director": true,
  "Principal Secretary": true,
  "Theatre": true,
  "Media Adviser": true,
  "Webmaster": true,
  "Head Coach": [
    "Baseball", 
    "Basketball - Boys", 
    "Basketball", "Volleyball - Girls", 
    "Volleyball", 
    "Spiritline"
  ]
}

以下是使用此功能的一个简单示例:

Object.keys(roles).forEach(role => {
  const value = roles[role];

  if (Array.isArray(value)) {
    console.log(role, 'of', value.join(', '));
  } else if (value === true) {
    console.log(role);
  } else {
    console.error(role, "has unknown value");
  }
});