array in json code example

Example 1: Javascript object to JSON string

var person={"first_name":"Tony","last_name":"Hawk","age":31};
var personJSONString=JSON.stringify(person);

Example 2: json example array

{
	"name":"John",
	"age":30,
	"cars":[ "Ford", "BMW", "Fiat" ]
}

// Another exemple
{
    "people": [
        {
            "website": "stackabuse.com",
            "from": "Nebraska",
            "name": "Scott"
        },
        {
            "website": "google.com",
            "from": "US",
            "name": "Zuck"
        }
    ]
}

Example 3: json arrays

{
  "array": [{1,2,3,4,5,6}]
}

Example 4: js create json array

var employees = {
    accounting: []
};

for(var i in someData) {    

    var item = someData[i];   

    employees.accounting.push({ 
        "firstName" : item.firstName,
        "lastName"  : item.lastName,
        "age"       : item.age 
    });
}

Example 5: json object

var myObj, x;
myObj = {"name":"John", "age":30, "car":null};
x = myObj.name;
document.getElementById("demo").innerHTML = x;

Example 6: hwo to access arary in json

console.log(dataJS);
console.log(dataJS[0].eee1);
console.log(dataJS[0].comments[0]);

Tags:

Java Example