我浏览了维基百科,用谷歌搜索了一下,并阅读了官方文档,但我还没有真正理解JSON是什么,以及为什么要使用它.

I have been building applications using PHP, MySQL and JavaScript / HTML for a while, and if JSON can do something to make my life easier or my code better or my user interface better, then I'd like to know about it. Can someone give me a succinct explanation?

推荐答案

JSON (JavaScript Object Notation) is a lightweight format that is used for data interchanging. It is based on a subset of JavaScript language (the way objects are built in JavaScript). As stated in the MDN, some JavaScript is not JSON, and some JSON is not JavaScript.

使用此功能的一个示例是Web服务响应.在"旧"时代,Web服务使用XML作为它们传回数据的主要数据格式,但是自从JSON出现(The JSON format is specified in 100 by Douglas Crockford)以来,它一直是首选格式,因为它更接近lightweight

你可以在官方JSON web site上找到更多信息.

JSON构建在两种 struct 之上:

  • 名称/值对的集合.在各种语言中,这是作为对象、记录、 struct 、字典、哈希表、键控列表或关联数组实现的.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

JSON struct



JSON Object diagram

JSON Array diagram

JSON Value diagram

JSON String diagram

JSON Number diagram

以下是一个JSON数据示例:

{
     "firstName": "John",
     "lastName": "Smith",
     "address": {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": 10021
     },
     "phoneNumbers": [
         "212 555-1234",
         "646 555-4567"
     ]
 }

JSON in JavaScript

JSON (in Javascript) is a string!

人们通常认为所有Javascript对象都是JSON,JSON是一个Javascript对象.这是不正确的.

In Javascript var x = {x:y} is not JSON, this is a Javascript object. The two are not the same thing. The JSON equivalent (represented in the Javascript language) would be var x = '{"x":"y"}'. x is an object of type string not an object in it's own right. To turn this into a fully fledged Javascript object you must first parse it, var x = JSON.parse('{"x":"y"}');, x is now an object but this is not JSON anymore.

See Javascript object Vs JSON


在使用JSON和JavaScript时,可能会try 使用eval函数来计算回调中返回的结果,但不建议这样做,因为有两个字符(U+2028和U+2029)在JSON中有效,但在JavaScript中无效(请阅读本here中的更多内容).

Therefore, one must always try to use Crockford's script that checks for a valid JSON before evaluating it. Link to the script explanation is found here and here is a direct link to the js file. Every major browser nowadays has its own implementation for this.

关于如何使用JSON解析器的示例(使用上述代码片段中的JSON):

//The callback function that will be executed once data is received from the server
var callback = function (result) {
    var johnny = JSON.parse(result);
    //Now, the variable 'johnny' is an object that contains all of the properties 
    //from the above code snippet (the json example)
    alert(johnny.firstName + ' ' + johnny.lastName); //Will alert 'John Smith'
};

The JSON parser also offers another very useful method, stringify. This method accepts a JavaScript object as a parameter, and outputs back a string with JSON format. This is useful for when you want to send data back to the server:

var anObject = {name: "Andreas", surname : "Grech", age : 20};
var jsonFormat = JSON.stringify(anObject);
//The above method will output this: {"name":"Andreas","surname":"Grech","age":20}

The above two methods (parse and stringify) also take a second parameter, which is a function that will be called for every key and value at every level of the final result, and each value will be replaced by result of your inputted function. (More on this here)

顺便说一句,对于所有认为JSON只适用于JavaScript的人来说,请查看this post篇文章,它对此进行了解释并证实了这一点.


参考文献

Json相关问答推荐

Elasticsearch Go客户端错误:无效的SON格式:在中间件中索引文档时出错

从Razor Pages的AJAX Json呈现DataTables问题.Net GET

Jolt变换将字段移动到数组的每个元素中

nlohmann json:为什么我会得到一个解析错误?

使用 jolt 变换压平具有公共列 JSON 的复杂嵌套

jq :遍历 json 并在键存在时检索集合

如何在 JSonPath 中按值查找列表中的所有元素

父键中的 Perl JSON 数组

golang递归json来构造?

在 JOLT 中重新排列值

坚持弄清楚如何使用 api 响应来调用以从不同的链接检索响应

使用 jq 同时迭代数组

如何判断字符串是否为json格式

在 Django 1.9 中,使用 JSONField(本机 postgres jsonb)的约定是什么?

将文件发送到 Rails JSON API

Jackson:忽略 Json 配置值

在 JSON.NET 中序列化派生类时的字段顺序

C++:使用 nlohmann json 从文件中读取 json 对象

Newtonsoft 对象 → 获取 JSON 字符串

按键值过滤 JSON