JSON (JavaScript Object Notation)
JSON is a light-weight text-based open standard designed for human-readable data. It is the most widely used format for interchanging data on the web. It originates from the JavaScript language and is represented with two primary data structures: ordered lists (recognized as 'arrays') and name/value pairs (recognized as 'objects').Previous post Explained about How to convert json to class in c#
Why JSON?
The JSON standard is language-independent and its data structures, arrays and objects, are universally recognized. These structures are supported in some way by nearly all modern programming languages and are familiar to nearly all programmers. These qualities make it an ideal format for data interchange on the web.Why Not XML?
The XML specification does not match the data model for most programming languages which makes it slow and tedious for programmers to parse. Compared to JSON, XML has a low data-to-markup ratio which results in it being more difficult for humans to read and write.Examples
Array
Array elements are generally of a basic type (number, string, boolean, or null), however can also be a nested array or object (see Data Structures). Elements are comma-delimited and contained within brackets.myArray = [ "John Doe", 29, true, null ]
myArray = [ [], {} ]myArray[2]
true
Array with objects
This array contains comma-delimited objects which each contain multiple comma-delimited key/value pairs. Objects within an array can be accessed using the array name and index.myArray = [ { "name": "John Doe", "age": 29 }, { "name": "Anna Smith", "age": 24 }, { "name": "Peter Jones", "age": 39 } ]myArray[0].name
John Doe
Object
This object contains multiple comma-delimited key/value pairs. Object properties can be accessed using the the object 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).myObject = { "first": "John", "last": "Doe", "age": 39, "sex": "M", "salary": 70000, "registered": true }myObject.salary
70000
myObject["salary"]
70000
Object with nested array
This object contains multiple 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.myObject = { "first": "John", "last": "Doe", "age": 39, "sex": "M", "salary": 70000, "registered": true, "interests": [ "Reading", "Mountain Biking", "Hacking" ] }myObject.interests[0]
Reading
myObject["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.myObject = { "first": "John", "last": "Doe", "age": 39, "sex": "M", "salary": 70000, "registered": true, "favorites": { "color": "Blue", "sport": "Soccer", "food": "Spaghetti" } }myObject.favorites.food
Spaghetti
myObject["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.myObject = { "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": "JavaScript", "tests": [ { "name": "One", "score": 90 }, { "name": "Two", "score": 96 } ] }, { "category": "CouchDB", "tests": [ { "name": "One", "score": 79 }, { "name": "Two", "score": 84 } ] }, { "category": "Node.js", "tests": [ { "name": "One", "score": 97 }, { "name": "Two", "score": 93 } ] } ] }myObject.skills[0].category
JavaScript
myObject["skills"][0]["category"]
JavaScript
myObject.skills[1].tests[0].score
79
myObject["skills"][1]["tests"][0]["score"]
79
Data Types
Number
Double-precision floating-point format.
var myNum = 123.456
Double-precision floating-point format.
String
Series of characters (letters, numbers, or symbols); double-quoted UTF-8 with backslash escaping.
var myString = "abcdef"
Series of characters (letters, numbers, or symbols); double-quoted UTF-8 with backslash escaping.
Boolean
One of two values: true or false.
var myBool = true
One of two values: true or false.
Array
Sequence of values, comma-separated and enclosed in square brackets; values don't need to be of the same type.
var myArray = [ "a", "b", "c", "d" ]
Sequence of values, comma-separated and enclosed in square brackets; values don't need to be of the same type.
Object
Unordered collection of key/value pairs; comma-separated and enclosed in curly braces; the should be strings and be distinct.
var myObject = { "id": 7 }
Unordered collection of key/value pairs; comma-separated and enclosed in curly braces; the should be strings and be distinct.
Null
Empty
Source from :
http://stackoverflow.com/questions/383692/what-is-json-and-why-would-i-use-it
var myNull = null
Empty
Source from :
http://stackoverflow.com/questions/383692/what-is-json-and-why-would-i-use-it
Comments
Post a Comment