Monday, September 26, 2016

JSON JS

What is JSON JS?

JSON stands for JavaScript Object Notation. JSON is lightweight data interchange format. JSON is language independent. JSON is easy to understand. The JSON syntax is derived from JavaScript object notation syntax, but the JSON format is text only. Code for reading and generating JSON data can be written in any programming language.

Why knockout JS?
  • It is used while writing JavaScript based applications that includes browser extensions and websites.
  • JSON format is used for serializing and transmitting structured data over network connection. 
  • It is primarily used to transmit data between a server and web applications. 
  • Web services and APIs use JSON format to provide public data. 
  • It can be used with modern programming languages.
How to use knockout JS?

Storing JSON Data
As a simple example, information about me might be written in JSON as follows:


var jason = {
                "age" : "24",
       "hometown" : "Missoula, MT",
                "gender" : "male"
};


Storing JSON Data in Arrays
A slightly more complicated example involves storing two people in one variable. To do this, we enclose multiple objects in square brackets, which signifies an array. For instance, if I needed to include information about myself and my brother in one variable, I might use the following:


var family = [{
       "name" : "Jason",
       "age" : "24",
                "gender" : "male"
};
{
                "name" : "Kyle",
                "age" : "21",
       "gender" : "male"
};


How Do We Load JSON into a Project?

One of the easiest ways to load JSON data into our web applications is to use the $.ajax() method available in the jQuery library. The ease of retrieving data will vary based on the site providing the data, but a simple example might look like this:


$.ajax(
       type:'GET',
       url:"http://example.com/users/feeds/",
       data:"format=json&id=123",
       success:function(feed) {
       document.write(feed);
       },
dataType:'jsonp'
);


Other interesting things to know?

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.

These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable with programming languages also be based on these structures.


No comments: