Sunday, June 11, 2017

JSON


  • JSON stands for JavaScript Object Notation
  • JSON is text, written with JavaScript object notation.
  • JSON is a lightweight data-interchange format
  • JSON is language independent 
  • It is easy for humans to read and write and understand.
  Exchanging Data s

When exchanging data between a browser and a server, the data can only be text. JSON is text, and we can convert any JavaScript object into JSON, and send JSON to the server. We can also convert any JSON received from the server into JavaScript objects.

Storing Data

When storing data, the data has to be a certain format, and regardless of where you choose to store it, text is always one of the legal formats. JSON helps to convert data and helps to store it.

JSON Tutorial


JSON Syntax Rules

  1. Curly braces hold objects
    • var person = "name":"Mala""age":19"city":"Jaffna" };
  2. Data is in name/value pairs
    • "Place":"Jaffna"
  3. Data is separated by commas
  4. String Values must be written with double quotes.
  5. Square brackets hold arrays

JSON has 2 inbuild functions to convert
  1. Parse
    • Convert String to JS
    • Parse the data with JSON.parse(), and the data becomes a JavaScript object.
    • var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');

  2. Stringfy
    • Converts JS to String
    • Convert a JavaScript object into a string with JSON.stringify().
    • var myJSON = JSON.stringify(obj);
JSON Data-Types

In JavaScript there are 9 datatypes
  1. String
  2. Number
  3. Boolean
  4. Function
  5. Objects
  6. Array
  7. Undefined 
  8. Null
  9. Date
But in JSON there only some of then can be accessed
  1. String
    • "name":"Mino" }
  2. Number
    • "age":20 }
  3. Boolean
    • "Job":"False" }
  4. Array
    • {
      "student":[ "Mala""Kala""Bala" ]
      }
  5. Objects
    • {
      "student":{ "name":"Mala""age":20"city":"Jaffna" }
      }
  6. Null
    • "middlename":null }

JSON Objects
  • {
    "student":{ "name":"Mala""age":20"city":"Jaffna" }
    }
  • Objects are surrounded by curly braces {}
  • Objects are written in key/value pairs
  • Keys and values are separated by a colon
  • Each key/value pair is separated by a comma
  • Keys must be strings, and values must be a valid JSON data type
Accessing Objects
  1. dot (.)
    • x = student.name;
  2. bracket ([])
    • x = student["name"];

JSON Array

  • {
    "student":[ "Mala""Kala""Bala" ]
    }
  • {
    "name":"Mala",
    "age":20,
    "subjects":[ "Maths""Science""English" ]
    }
Accessing Objects
  1. calling index in bracket ([])
    • x = student.subjects[0];
Modifying Objects
  •  student.subject[1] = "ICT";
Deleting Objects
  •  delete student.subject[1];
  • Nested Arrays
    • Values in an array can also be another array
    • student= {
          "name":"Kala",
          "age":18,
          "cars": [
              "name":"Maths""models":[ "Part 1""Part 2"] },
              "name":"Science""models":[ "Part 1", "Part 2" ] }
          ]
       }


No comments:

Post a Comment

My First Day with Uki DN Cohort 1

 "Hello WORLD!"