What is JSON and why would I use it?

前端 未结 17 1864
说谎
说谎 2020-11-21 15:54

I\'ve looked on wikipedia and Googled it and read the official documentation, but I still haven\'t got to the point where I really understand what JSON is, and why I\'d use

17条回答
  •  名媛妹妹
    2020-11-21 16:10

    The difference between JSON and conventional syntax would be as follows (in Javascript)

    Conventional

     function Employee(name, Id, Phone, email){
    
          this.name = name;
          this.Id = Id;
          this.Phone = Phone;
          this.email = email;
      }
    
      //access or call it as 
    
    var Emp = new Employee("mike","123","9373849784","mike.Anderson@office.com");
    

    With JSON

    if we use JSON we can define in different way as

      function Employee(args){
    
       this.name = args.name;
       this.Id = args.Id;
       this.Phone = args.Phone;
       this.email = args.email;
    }
    
    //now access this as...
    
    var Emp = new Employee({'name':'Mike', 'Id':'123', 'Phone':'23792747', 'email':'mike.adnersone@office.com'});
    

    The important thing we have to remember is that, if we have to build the "Employee" class or modal with 100 elements without JSON method we have to parse everything when creating class. But with JSON we can define the objects inline only when a new object for the class is defined.

    so this line below is the way of doing things with JSON(just a simple way to define things)

     var Emp = new Employee({'name':'Mike', 'Id':'123', 'Phone':'23792747', 'email':'mike.adnersone@office.com'});
    

提交回复
热议问题