Why your proposed result is invalid
Your proposed result is invalid. It can never exist.
Objects (as indicated by curly braces {...}
) are essentially dictionaries. They map keys to values. It is therefore invalid to not specify any keys (as you did).
The data structure you are seeking for are arrays, which are indicated by square brackets ([...]
).
You can of course mimic array-like behavior by specifying numbered indices in your dictionary:
var obj = {
0: firstElement,
1: secondElement
};
// the same as*
var arr = [firstElement, secondElement];
*) Note that obj
and arr
are not equivalent! While arrays are objects, objects at their raw form do not provide array functions, e.g. obj.length
will be undefined.
JSON != Object literal notation
You seem to confuse the term JSON with the syntax you are using in your JavaScript code, which is called Object literal notation
JSON
It is a data format.
{
"key": "value"
}
// or
[
"first",
"second"
]
...
Double quotes around keys are required. Consult http://json.org for more information on the syntax.
Object Literals
Those are embedded in JavaScript code.
// JS code
alert("Test, this is JS code"); // sample
var obj = {
1: "abc",
2: ["first", "second"]
};
Double quotes around keys are optional in this case.
solved How to convert Array into JSON [closed]