Monday, October 29, 2012

JSON [JavaScript Object Notation]

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.


  • JSON is syntax for storing and exchanging text information. Much like XML.
  • JSON is smaller than XML, and faster and easier to parse.
JSON Example
{
"employees": [
{ "firstName":"Dibakar" , "lastName":"Ray" },
{ "firstName":"Sujit" , "lastName":"Banerjee" },
{ "firstName":"Anadomohan" , "lastName":"Chakraborty" }
]
}


Working with JSON Data ASP. Net

{"IsMember" : true, "Name" : "Surnider", "Age" : 24}
Define the User type to deserialize the JSON into.

public class User
{
    public bool IsMember { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}
DataContractJsonSerializer serializer =
    new DataContractJsonSerializer(typeof(User));
User user = (User)serializer.ReadObject(responseStream);

bool isMember = user.IsMember;
string name = user.Name;
int age = user.Age;

No comments: