How to use Asp.net, MVC ,Jquery,SqlServer,Firebird,Java Script in computer Programming language
Monday, May 20, 2013
JSON Examples
JSON Examples
Array
Array typically consist of number, string, boolean, or null values and can also include nested arrays and nested objects. An array's elements are comma-delimited and are contained in brackets.
var newArray = [ "Text goes here", 29, true, null ]
var otherArray = [ [...], {...} ]
console.log( newArray[2] );true
Array with objects
This array consists of comma-delimited, nested objects which ceach ontain multiple key/value pairs- also comma-delimited. These objects can be accessed using the array's variable name and an index.
var newArray = [
{ "name": "Dagny Taggart", "age": 39 },
{ "name": "Francisco D'Anconia", "age": 40 },
{ "name": "Hank Rearden", "age": 46 }
]
console.log( newArray[0].name );Dagny Taggart
Object
This object consists of several key/value pairs. These 'properties' can be accessed using the the object's variable name followed by a period and property name -or- can be accessed like an array using the property name in quotes (in place of an index).
newObject = {
"first": "Jimmy",
"last": "James",
"age": 29,
"sex": "M",
"salary": 63000,
"registered": false
}
console.log( newObject.salary );63000
console.log( newObject["salary"] );63000
Object with nested array
This object contains comma-delimited key/value pairs and a nested array. The nested array can be accessed with the object name, property or 'key' containing the array and an array index.
newObject = {
"first": "John",
"last": "Doe",
"age": 39,
"sex": "M",
"salary": 70000,
"registered": true,
"interests": [ "Reading", "Mountain Biking", "Hacking" ]
}
console.log( newObject.interests[0] );Reading
console.log( newObject["interests"][0] );Reading
Object with nested object
This object contains multiple comma-delimited key/value pairs and a nested object. To access an object within an object, property names or 'keys' can be chained together -or- property names can be used like an array in place of indexes.
newObject = {
"first": "John",
"last": "Doe",
"age": 39,
"sex": "M",
"salary": 70000,
"registered": true,
"favorites": {
"color": "Blue",
"sport": "Soccer",
"food": "Spaghetti"
}
}
console.log( newObject.favorites.food );Spaghetti
console.log( newObject["favorites"]["food"] );Spaghetti
Object with nested arrays and objects
This object is more complex and typical of what might come from an API. It contains key/value pairs, nested arrays and nested objects. Any of its elements can be accessed with a combination of the above techniques.
newObject = {
"first": "John",
"last": "Doe",
"age": 39,
"sex": "M",
"salary": 70000,
"registered": true,
"interests": [ "Reading", "Mountain Biking", "Hacking" ],
"favorites": {
"color": "Blue",
"sport": "Soccer",
"food": "Spaghetti"
},
"skills": [
{
"category": "PHP",
"tests": [
{ "name": "One", "score": 90 },
{ "name": "Two", "score": 96 }
]
},
{
"category": "CouchDB",
"tests": [
{ "name": "One", "score": 32 },
{ "name": "Two", "score": 84 }
]
},
{
"category": "Node.js",
"tests": [
{ "name": "One", "score": 97 },
{ "name": "Two", "score": 93 }
]
}
]
}
console.log( newObject.skills[0].category );PHP
console.log( newObject["skills"][0]["category"] );PHP
console.log( newObject.skills[1].tests[0].score );32
console.log( newObject["skills"][1]["tests"][0]["score"] );32
Ref http://www.jsonexample.com/#array-with-objects
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment