What is JSON and why would I use it?

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

提交回复
热议问题