Reputation: 423
I'm learning JSON
and its structure. I have read documentation but still feel a bit confused on the the different structures and what they mean and how they are set up.
Let's take this for an example:
[
{"firstName":"Jocke","job":"developer"},
{"firstName":"Anna","job":"skydiver"},
{"firstName":"Peter","job":"pilot"}
]
Now, this is valid. Is this counted as an object? The reason I ask is because of the brackets []
that I normally associate with arrays. So if (I assume) this is an object, then is the code below an array?
[ "value1", "value2", "value3" ]
The difference being not using {}
and :
Now if I'm right on the two first (object, array), then I'm on track, but there is still one thing I don't quite grasp. I have seen this structure as well:
{ "people":
[
{"firstName":"Jocke","job":"developer"},
{"firstName":"Anna","job":"skydiver"},
{"firstName":"Peter","job":"pilot"}
]
}
This is also valid, and I assume it's still an object. What confuse me is "people"
. What is that in the context of an object, why is it there, and why would I need it?
Upvotes: 1
Views: 264
Reputation: 909
First one is an array of objects.
[
{"firstName":"Jocke","job":"developer"},
{"firstName":"Anna","job":"skydiver"},
{"firstName":"Peter","job":"pilot"}
]
The one you showed in the end is a Json object which has one Object which is People
and People
itself is an Array of objects.
{ "people":
[
{"firstName":"Jocke","job":"developer"},
{"firstName":"Anna","job":"skydiver"},
{"firstName":"Peter","job":"pilot"}
]
}
An array is treated as a special object and is a first class citizen in Json.
In short, Every place where you can use Objects, You can also use array of Objects.
Upvotes: 1
Reputation: 38189
The JavaScript Array class is a global object that is used in the construction of arrays; which are high-level, list-like objects.
It is an array of objects:
[
{"firstName":"Jocke","job":"developer"},
{"firstName":"Anna","job":"skydiver"},
{"firstName":"Peter","job":"pilot"}
]
Array of strings:
[ "value1", "value2", "value3" ]
An object with property people
which contains array:
{ "people":
[
{"firstName":"Jocke","job":"developer"},
{"firstName":"Anna","job":"skydiver"},
{"firstName":"Peter","job":"pilot"}
]
}
Upvotes: 2