问题描述
我以前从未使用过JSON,所以我对它的语法不熟悉.
目前,我有多个包含不同数据的数组.
我想创建一个JSON对象,其中包含多个数组,每个数组包含几条数据.
例如
一个名为cars的对象,其中包含多个数组,每个数组用于不同的car品牌.在每个阵列中将是汽车的模型以及一些其他类型的数据,例如门的数量(这只是一个虚构的例子,并不重要).
如果有人用示例解释语法,将不胜感激.
在最外层,JSON对象以{
开头,以}
结尾.
样本数据:
{
"cars": {
"Nissan": [
{"model":"Sentra", "doors":4},
{"model":"Maxima", "doors":4},
{"model":"Skyline", "doors":2}
],
"Ford": [
{"model":"Taurus", "doors":4},
{"model":"Escort", "doors":4}
]
}
}
如果将JSON分配给名为data的变量,则对其进行访问将类似于以下内容:
data.cars['Nissan'][0].model // Sentra
data.cars['Nissan'][1].model // Maxima
data.cars['Nissan'][2].doors // 2
for (var make in data.cars) {
for (var i = 0; i < data.cars[make].length; i++) {
var model = data.cars[make][i].model;
var doors = data.cars[make][i].doors;
alert(make + ', ' + model + ', ' + doors);
}
}
另一种方法(对汽车模型使用关联数组而不是索引数组):
{
"cars": {
"Nissan": {
"Sentra": {"doors":4, "transmission":"automatic"},
"Maxima": {"doors":4, "transmission":"automatic"}
},
"Ford": {
"Taurus": {"doors":4, "transmission":"automatic"},
"Escort": {"doors":4, "transmission":"automatic"}
}
}
}
data.cars['Nissan']['Sentra'].doors // 4
data.cars['Nissan']['Maxima'].doors // 4
data.cars['Nissan']['Maxima'].transmission // automatic
for (var make in data.cars) {
for (var model in data.cars[make]) {
var doors = data.cars[make][model].doors;
alert(make + ', ' + model + ', ' + doors);
}
}
更正:JSON对象以{
开头并以}
结束,但是具有一个以[
开头并以]
结束的JSON数组(在最外层)也是有效的. /p>
此外,原始JSON数据中的重大语法错误也已得到纠正:JSON对象中的所有键名都必须用双引号引起来,并且JSON对象或JSON数组中的所有字符串值也必须用双引号引起来.
请参阅:
I've never used JSON before so I'm not familiar with its syntax.
At the moment I have multiple arrays containing different pieces of data.
I would like to create one JSON object, that contains the multiple arrays each with several pieces of data.
E.g.
An object called cars, containing multiple arrays each for a different make of car. In each array would be the model of car along with some other types of data e.g. number of doors (doesn't really matter its just a fictional example.)
It would be greatly appreciated if someone explained the syntax with an example.
On the outermost level, a JSON object starts with a {
and end with a }
.
Sample data:
{
"cars": {
"Nissan": [
{"model":"Sentra", "doors":4},
{"model":"Maxima", "doors":4},
{"model":"Skyline", "doors":2}
],
"Ford": [
{"model":"Taurus", "doors":4},
{"model":"Escort", "doors":4}
]
}
}
If the JSON is assigned to a variable called data, then accessing it would be like the following:
data.cars['Nissan'][0].model // Sentra
data.cars['Nissan'][1].model // Maxima
data.cars['Nissan'][2].doors // 2
for (var make in data.cars) {
for (var i = 0; i < data.cars[make].length; i++) {
var model = data.cars[make][i].model;
var doors = data.cars[make][i].doors;
alert(make + ', ' + model + ', ' + doors);
}
}
Another approach (using an associative array for car models rather than an indexed array):
{
"cars": {
"Nissan": {
"Sentra": {"doors":4, "transmission":"automatic"},
"Maxima": {"doors":4, "transmission":"automatic"}
},
"Ford": {
"Taurus": {"doors":4, "transmission":"automatic"},
"Escort": {"doors":4, "transmission":"automatic"}
}
}
}
data.cars['Nissan']['Sentra'].doors // 4
data.cars['Nissan']['Maxima'].doors // 4
data.cars['Nissan']['Maxima'].transmission // automatic
for (var make in data.cars) {
for (var model in data.cars[make]) {
var doors = data.cars[make][model].doors;
alert(make + ', ' + model + ', ' + doors);
}
}
Edit:
Correction: A JSON object starts with {
and ends with }
, but it's also valid to have a JSON array (on the outermost level), that starts with [
and ends with ]
.
Also, significant syntax errors in the original JSON data have been corrected: All key names in a JSON object must be in double quotes, and all string values in a JSON object or a JSON array must be in double quotes as well.
See:
这篇关于如何制作具有多个数组的JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!