Jump to: navigation, search

JSON Basic

From w3cyberlearnings

Contents

What is JSON?

  • JSON stands for JavaScript Object Notation.
  • JSON is lightweight implementation of data.
  • JSON native supports in JavaScript and it is very fast.

Syntax JSON

var jobject={}

Example 1

var jobject = {'name':'Mark'};
alert(jobject.name); // alert Mark

Example 2

var jobject ={'name':'Mark','age':13};

alert(jobject.age); // alert 13

Example 3

var jobject ={
    'name':'Mark',
    'age':13
    'class':'A30',
    'course':'Math20'
};

alert(jobject.class); // A30
alert(jobject.course); // Math20

Example 4: List students and teachers

var jobject = {
   'students': [
        {'name':'Joma','age':12, 'class':'A31','course':'Math10'},
        {'name':'John','age':11, 'class':'A31','course':'Math10'},
        {'name':'Joli','age':13, 'class':'A30','course':'Math11'},
        {'name':'Alex','age':12, 'class':'A30','course':'Math11'},
               ],
   'teacher':[
        {'name':'Mark','course':'Math10'},
        {'name':'Jason','course':'Math11'},
        ]
    };

alert(jobject.students[0].name); // alert Joma
alert(jobject.students[0].age); // alert 12
alert(jobject.students[1].name); // alert John


Example 5: Display student record with address information

var jobject = {
      'name': 'Joma',
      'age':12,
      'address': {
           'street':'240 Lake Rd',
           'apt':210
       }   
  }

alert(jobject.name); // alert joma
alert(jobject.address.street); // alert 240 Lake Rd
alert(jobject.address.apt); // alert 210

Example 6: Multiple JSON data

  • Two records
var jobject = [
   {
      'name': 'Alex',
      'age':12,
      'address': {
           'street':'String Lake Rd',
           'apt':89
       }   
   },
   {
      'name': 'Joma',
      'age':12,
      'address': {
           'street':'240 Lake Rd',
           'apt':210
       }   
   }
];

alert(jobject[1].name); // alert Joma
alert(jobject[0].name); // alert Alex
alert(jobject[1].address.street); // alert 240 Lake Rd
alert(jobject[1].address.apt); // alert 210
Navigation
Web
SQL
MISC
References