What is JSON and why would I use it?

前端 未结 17 1851
说谎
说谎 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:07

    What is JSON?

    JavaScript Object Notation (JSON) is a lightweight data-interchange format inspired by the object literals of JavaScript.

    JSON values can consist of:

    objects (collections of name-value pairs) arrays (ordered lists of values) strings (in double quotes) numbers true, false, or null

    JSON is language independent.

    JSON with PHP?

    After PHP Version 5.2.0, JSON extension is decodes and encodes functionalities as default.

    Json_encode - returns the JSON representation of values Json_decode - Decodes the JSON String Json_last_error - Returns the last error occured.

    JSON Syntax and Rules?

    JSON syntax is derived from JavaScript object notation syntax:

    Data is in name/value pairs Data is separated by commas Curly braces hold objects Square brackets hold arrays

    0 讨论(0)
  • 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'});
    
    0 讨论(0)
  • 2020-11-21 16:10

    It's very simple. JSON stands for Java Script Object Notation. Think of it as an alternative to using XML for transferring data between software components.

    For example, I recently wrote a bunch of web services that returned JSON, and some Javascript developers then wrote code which called the services and consumed the information returned in that format.

    0 讨论(0)
  • 2020-11-21 16:12

    the common short answer is: if you are using AJAX to make data requests, you can easily send and return objects as JSON strings. Available extensions for Javascript support toJSON() calls on all javascript types for sending data to the server in an AJAX request. AJAX responses can return objects as JSON strings which can be converted into Javascript objects by a simple eval call, e.g. if the AJAX function someAjaxFunctionCallReturningJson returned

    "{ \"FirstName\" : \"Fred\", \"LastName\" : \"Flintstone\" }"
    

    you could write in Javascript

    var obj = eval("(" + someAjaxFunctionCallReturningJson().value + ")");
    alert(obj.FirstName);
    alert(obj.LastName);
    

    JSON can also be used for web service payloads et al, but it is really convenient for AJAX results.

    • Update (ten years later): Don't do this, use JSON.parse
    0 讨论(0)
  • 2020-11-21 16:12

    We have to do a project on college and we faced a very big problem, it is called Same Origin Policy. Amog other things, it makes that your XMLHttpRequest method from Javascript can't make requests to domains other than the domain that your site is on.

    For example you can't make request to www.otherexample.com if your site is on www.example.com. JSONRequest allows that, but you will get result in JSON format if that site allows that(for example it has a web service that returns messages in JSON). That is one problem where you could use JSON perhaps.

    Here is something practical: Yahoo JSON

    0 讨论(0)
  • 2020-11-21 16:14

    Try the following code to parse your php json response: read.php

    <script
      src="https://code.jquery.com/jquery-3.2.1.min.js"
      integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
      crossorigin="anonymous"></script>
    <script type="text/javascript">  
    $.ajax({
        url:'index.php',
        data:{},
        type:"POST",
        success:function(result) {
            jsondecoded = $.parseJSON(result);
            $.each(jsondecoded, function(index, value) {
                $("#servers").text($("#servers").text() + " " + value.servername);
                console.log(value.start);
                console.log(value.end);
                console.log(value.id);
            });
        },
        statusCode: {
        404: function() {
          alert( "page not found" );
        }
      }
    });
    </script>
    

    server.php

    <?php 
    echo '[{"start":"2017-08-29","end":"2017-09-01","id":"22"},{"start":"2017-09-03","end":"2017-09-06","id":"23"}]';
    ?>
    
    0 讨论(0)
提交回复
热议问题