Skip to main content

What is JSON (JavaScript Object Notation)

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].nameJohn 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.salary70000

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.foodSpaghetti

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].categoryJavaScript

myObject["skills"][0]["category"]JavaScript

myObject.skills[1].tests[0].score79

myObject["skills"][1]["tests"][0]["score"]79

Data Types

Numbervar myNum = 123.456
Double-precision floating-point format.
Stringvar myString = "abcdef"
Series of characters (letters, numbers, or symbols); double-quoted UTF-8 with backslash escaping.
Booleanvar myBool = true
One of two values: true or false.
Arrayvar 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.
Objectvar myObject = { "id": 7 }
Unordered collection of key/value pairs; comma-separated and enclosed in curly braces; the should be strings and be distinct.
Nullvar myNull = null
Empty

Source from :

http://stackoverflow.com/questions/383692/what-is-json-and-why-would-i-use-it

Comments

Popular posts from this blog

How to hide url parameters in asp.net

There are different ways to Hide the URL in asp.net , you can choose any one from bellow options . Previously I was Explained about the  Difference between Convert.tostring and .tostring() method Example  ,   Reasons to use Twitter Bootstrap , How to Register AJAX toolkit in web.config file in asp.net a) Using Post Method b) Using Of Session . c) URL Encoding & decoding process . d) Using Server.Transfer() instead of Response.Redirect() method (1)Use a form and POST the information. This might require additional code in source pages, but should not require logic changes in the target pages (merely change Request.QueryString to Request.Form). While POST is not impossible to muck with, it's certainly less appealing than playing with QueryString parameters. (2)Use session variables to carry information from page to page. This is likely a more substantial effort compared to (1), because you will need to take session variable checking into account...

ASP.NET Routing

ASP.NET routing enables you to use URLs that do not have to map to specific files in a Web site. Because the URL does not have to map to a file, you can use URLs that are descriptive of the user's action and therefore are more easily understood by users. The ASP.NET MVC framework and ASP.NET Dynamic Data extend routing to provide features that are used only in MVC applications and in Dynamic Data applications. For more information about MVC, see ASP.NET MVC 3 . For more information about Dynamic Data, see ASP.NET Dynamic Data Content Map . In an ASP.NET application that does not use routing, an incoming request for a URL typically maps to a physical file that handles the request, such as an .aspx file. For example, a request for http://server/application/Products.aspx?id=4 maps to a file that is named Products.aspx that contains code and markup for rendering a response to the browser. The Web page uses the query string value of id=4 to determine what type of c...

How to send mail asynchronously in asp.net with MailMessage

With Microsoft.NET Framework 2.0 everything is asynchronous and we can send mail also asynchronously. This features is very useful when you send lots of bulk mails like offers , Discounts , Greetings . You don’t have to wait for response from mail server and you can do other task . By using     SmtpClient . SendAsync Method (MailMessage, Object)    you need to do  System.Net.Mail has also added asynchronous support for sending email. To send asynchronously, you need need to Wire up a SendCompleted event Create the SendCompleted event Call SmtpClient.SendAsync smtpClient.send() will initiate the sending on the main/ui  thread and would block.  smtpClient.SendAsync() will pick a thread from the .NET Thread Pool and execute the method on that thread. So your main UI will not hang or block . Let's create a simple example to send mail. For sending mail asynchronously you need to create a event handler that will notify that mail success...