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 e-Commerce website GridView with Product Listing

Introduction : E-Commerce web applications are everywhere these days, and many share a common set of functionality. In this article, I will show how to use the GridView and ListView controls to build a powerful product page with many of the features found on today's e-commerce sites. We'll build a bicycle store product grid using some free clip art bicycle images. The example files are user controls which can be easily added to a page. We're only using three images here to keep the size of the sample application small. In previously I was explained about  Sending Email from Asp.net With Dynamic Content  ,  How To Export gridview data to Excel/PDF , CSV Formates in asp.net C#  , How to print Specific Area in asp.net web page How To- Search records or data in gridview using jQuery  . A shopping cart application would require to display the products in a multi column grid, rather than a straight down list or a table. The each item in a product grid would have

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 (e.g. the

Nested GridView in ASP.NET Using c# with show/hide

In This example shows how to create Nested GridView In Asp.Net Using C# And VB.NET With Expand Collapse Functionality. Previous post I was Explained about the   ASP.NET e-Commerce website GridView with Product Listing  ,  How To Export gridview data to Excel/PDF , CSV Formates in asp.net C# , Sending Email from Asp.net With Dynamic Content  ,  SQL Server- Case Sensitive Search in SQL Server I have used JavaScript to Create Expandable Collapsible Effect by displaying Plus Minus image buttons. Customers and Orders Table of Northwind Database are used to populate nested GridViews. Drag and place SqlDataSource from toolbox on aspx page and configure and choose it as datasource from smart tags Go to HTML source of page and add 2 TemplateField in <Columns>, one as first column and one as last column of gridview. Place another grid in last templateField column. Markup of page after adding both templatefields will like as shown below. HTML SOURCE < a