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

Asp.Net AjaxFileUpload Control With Drag Drop And Progress Bar

This Example explains how to use AjaxFileUpload Control With Drag Drop And Progress Bar Functionality In Asp.Net 2.0 3.5 4.0 C# And VB.NET. Previous Post  I was Explained about the   jQuery - Allow Alphanumeric (Alphabets & Numbers) Characters in Textbox using JavaScript  ,  Fileupload show selected file in label when file selected  ,  Check for file size with JavaScript before uploading  . May 2012 release of AjaxControlToolkit includes a new AjaxFileUpload Control  which supports Multiple File Upload, Progress Bar and Drag And Drop functionality. These new features are supported by Google Chrome version 16+, Firefox 8+ , Safari 5+ and Internet explorer 10 + , IE9 or earlier does not support this feature. To start with it, download and put latest AjaxControlToolkit.dll in Bin folder of application, Place ToolkitScriptManager  and AjaxFileUpload on the page. HTML SOURCE < asp:ToolkitScriptManager I...

View online files using the Google Docs Viewer

Use Google Docs Viewer for Document viewing within Browser I was looking for a way to let users see Microsoft Word Doc or PDF files online while using my application without leaving their browser without downloading files and then opening it to view with Word or PDF viewer . I was looking for some way out either via any PHP or Microsoft.NET libraries, did some googling on that; but later on I just got an idea that google already has all code written for me.. when I have any email attachment in PDF or DOC or DOCX google does it for me ..! Even while searching I can see PDFs by converting them in HTML. So I just googled it up and found that Google already has this ability that we can use Google Docs Viewer without any Google Account Login . YES that's true no Google Account login is required. It's damn simple and easy. Just pass document path as attachment as parameter and we are done. Google Docs Viewer gives us ability to embed PDF, DOC/DOCX, PPT, TIFF:...

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