2017-02-13 143 views
-4

我無法獲得下面的JSON來驗證。它崩潰在這裏:嵌套在JSON中

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

沒有最後一個數組沒有問題。有任何想法嗎?


{ 
    "userprofileid": 12547, 
    "email": "[email protected]", 
    "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"] 
     ] 
    }] 
} 
+0

任何JSON驗證器都應該指出發現錯誤的確切位置。 – 2017-02-13 03:59:18

回答

0

請嘗試以下

{ 「userprofileid」:12547, 「電子郵件」: 「[email protected]」, 「FIRST_NAME」: 「馬裏貝爾」, 「middle_name」:空, 「last_name」:「Vasquez」,「prefix」:null,「suffix」:null,「schools」:[{「schoolid」:417,「name」:「Clifton High School」,「addr」:4523 Appaloosa St 「,」addr2「:null,」city「:」Clifton「,」州「:」AZ「,」zip「:」85533「,」角色「:[」運動主任「,」首席祕書「 「,」媒體顧問「,」網站管理員「,」主教練「,[」棒球「,」籃球 - 男孩「,」籃球「,」排球 - 女孩「,」排球「,」Spiritline「]]]]}

"Head Coach":應該是"Head Coach", 這是格式化版本。

{ 
    "userprofileid":12547, 
    "email":"[email protected]", 
    "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" 
      ] 
     ] 
     } 
    ] 
} 
+0

您可以在https://jsonformatter.curiousconcept.com/中複製並粘貼JSON,它會告訴您您的JSON是否有效。 –

0

試試這個,這可以確保您有一個名爲'Head Coach'的參考。

{ 
    "userprofileid": 12547, 
    "email": "[email protected]", 
    "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" 
       ] 
      } 
     ] 
    }] 
} 
+0

這個JSON驗證了,但它可能不會做OP所要做的,也就是將像棒球這樣的各種運動與作爲「頭部教練」的角色聯繫起來。 – 2017-02-13 04:43:23

+0

我們不能在這個JSON中訪問主教練嗎?如果可以的話,我們也可以訪問列表成員。 – AndyrK

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"); 
    } 
}); 
+0

非常感謝幫助/課程@torazaburo。非常感謝。您對改變數據模型的建議很有意義。我會很快實施。 –