不使用列表创建 JSON 个对象
creating JSON object without using a list
我希望能够创建一个 JSON 对象,以便我可以像这样访问它。
education.schools.UNCC.graduation
目前我的JSON是这样的:
var education = {
"schools": [
"UNCC": {
"graduation": 2015,
"city": "Charlotte, NC",
"major": ["CS", "Spanish"]
},
"UNC-CH": {
"graduation": 2012,
"city": "Chapel Hill, NC"
"major": ["Sociology", "Film"]
}
],
"online": {
"website": "Udacity",
"courses": ["python", "java", "data science"]
}
};
当我使用 JSON 的 Lint 时,我收到一条错误消息。
我知道我可以重新格式化我的对象以像这样访问它(如下),但我不想这样做。我希望能够调用学校名称,而不是使用索引号。
education.schools[1].graduation
这是您的 JSON 更正。
您的 JSON 无效。
{
"schools": [
{
"UNCC": {
"graduation": "2015",
"city": [
"CS",
"Spanish"
],
"major": [
"CS",
"Spanish"
]
}
},
{
"UNC-CH": {
"graduation": "2012",
"city": [
"Chapel Hill",
"NC"
],
"major": [
"Sociology",
"Film"
]
}
}
],
"online": {
"website": "Udacity",
"courses": [
"python",
"java",
"data science"
]
}
}
解释:
- "city": "Chapel Hill, NC" -> 这是一个有 2 个值的数组 "Chapel Hill" 和“HC”,就像你对专业和课程所做的那样。
- Schools数组,需要用这个sintaxe构造数组[{
http://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html
对象有命名键。数组是成员列表。
用一个对象替换"schools"
的值。将 []
更改为 {}
。
我希望能够创建一个 JSON 对象,以便我可以像这样访问它。
education.schools.UNCC.graduation
目前我的JSON是这样的:
var education = {
"schools": [
"UNCC": {
"graduation": 2015,
"city": "Charlotte, NC",
"major": ["CS", "Spanish"]
},
"UNC-CH": {
"graduation": 2012,
"city": "Chapel Hill, NC"
"major": ["Sociology", "Film"]
}
],
"online": {
"website": "Udacity",
"courses": ["python", "java", "data science"]
}
};
当我使用 JSON 的 Lint 时,我收到一条错误消息。 我知道我可以重新格式化我的对象以像这样访问它(如下),但我不想这样做。我希望能够调用学校名称,而不是使用索引号。
education.schools[1].graduation
这是您的 JSON 更正。 您的 JSON 无效。
{
"schools": [
{
"UNCC": {
"graduation": "2015",
"city": [
"CS",
"Spanish"
],
"major": [
"CS",
"Spanish"
]
}
},
{
"UNC-CH": {
"graduation": "2012",
"city": [
"Chapel Hill",
"NC"
],
"major": [
"Sociology",
"Film"
]
}
}
],
"online": {
"website": "Udacity",
"courses": [
"python",
"java",
"data science"
]
}
}
解释:
- "city": "Chapel Hill, NC" -> 这是一个有 2 个值的数组 "Chapel Hill" 和“HC”,就像你对专业和课程所做的那样。
- Schools数组,需要用这个sintaxe构造数组[{
http://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html
对象有命名键。数组是成员列表。
用一个对象替换"schools"
的值。将 []
更改为 {}
。