I am trying to understand how a single and multidimensional javascript array would appear in JSON. Can anyone help me with an example for each?
Single-dimensional:
["one", "two", "three"]
Multi-dimensional:
[["one", "two", "three"],
["four", "five", "six"]]
Single array of primitive integers:
[1, 1, 2, 3, 5, 8]
Single array of objects:
[
{
"title": "hello",
"msg": "world"
},
{
"title": "stack",
"msg": "overflow"
},
{
"title": "json",
"msg": "array"
},
]
Multidimensional array of primitive integers:
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
I think you should know what's the difference between JSON and a JavaScript Object literal, they can look exactly the same, but there are some semantic differences.
JSON is a language-agnostic data interchange format, proposed by Douglas Crockford on 2006, its grammar differs from the JavaScript Object literals, basically by allowing only string keys and the values MUST be an object, array, number, string, or one of the literal names: false
, true
or null
.
Speaking about arrays, in JavaScript they can hold any type of value, primitive values like String
, Number
, Boolean
, undefined
or null
, and any type of object, even objects with methods, host objects like DOM elements, Date
objects and so on.
The syntax diagrams of JSON arrays and values might help you: