Thursday, May 18, 2017

JSON Arrays

Arrays as JSON Objects


"Ford""BMW""Fiat" ]
As you can learn from JSON formatter, Arrays in JSON are almost the same as in JavaScript.
Array values in JSON need to be of type string, number, object, array, boolean, or null.
Array values in Javascript can be all of the above and any other valid JavaScript expression such as functions, dates, and undefined.

Arrays in JSON Objects

Arrays can be values of an object property:
"cars":[ "Ford""BMW""Fiat" ]

Access Array Values

Access the array values by using the index number:
x =[0];

LoopThrough an Array

Access array values by using a for-in loop:
for (i in {
Or use a for loop:
for (i 0; i <; i++) {
    x +=[i];

Nested Arrays in JSON Objects

Values in an array can also be another array, or even another JSON object:
myObj = {
    "cars": [
        { "name":"Ford""models":[ "Fiesta""Focus""Mustang" ] },
        "name":"BMW""models":[ "320""X3""X5" ] },
        "name":"Fiat""models":[ "500""Panda" ] }
Use a for-in loop for each array in order to access arrays inside arrays:
for (i in {
    x += "<h1>" +[i].name "</h1>";
    for (j in[i].models) {
        x +=[i].models[j];

Modify Array Values

Use the index number to modify an array:[1] = "Mercedes";

Delete Array Items

Use the delete keyword to delete items from an array:

No comments:

Post a Comment