JSON arrays represent a collection of objects. In JS, theres a bunch of collection functions off of them such as slice
, pop
, push
. Objects have just more raw data.
A JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with commas separating the values.
A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names.
Object - key and value, Array - numerals, strings, booleans. When do you use this or that?
You can think of Arrays as "is a/an" and Objects as "has a". Lets use "Fruit" as example. Every item in fruit array is a type of fruit.
array fruit : [orange, mango, banana]
Arrays can contain objects,strings, numbers, arrays, but lets deal with only objects and arrays.
array fruit : [orange:[], mango:{}, banana:{}]
. You can see that orange is an array too. It implies any item that goes int orange is a type of orange, say: bitter_orange, mandarin, sweet_orange.
for fruit object, any item in it is an attribute of fruit. thus the fruit has a
object fruit :{seed:{}, endocarp:{},flesh:{}}
This also implies that anything within the seed object should be property of seed, say: colour, ..
JSON is primarily a language that allows serializing javascript objects into strings. So upon deserializing a JSON string you should get a javascript object structure. If your json deserializes into an object that stores 100 objects called object1 to object100 then that's going to be very inconvenient. Most deserializers will expect you to have known objects and arrays of known objects so that they can convert the strings into the actual object structure in the language you're using. Also this is a question that the philosophy of object oriented design would answer you.
credits to all particiapnts What are the differences between using JSON arrays vs JSON objects?