本文介绍了什么是JSON,我为什么要使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了维基百科,并用Google搜索了它,并阅读了官方文档,但是我还没有真正了解JSON是什么以及为什么要使用JSON的问题.

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 it.

一段时间以来,我一直在使用PHP,MySQL和Javascript/HTML来构建应用程序,如果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对象表示法)是一种轻量级格式,用于数据交换.基于JavaScript语言的子集(对象在JavaScript中的构建方式).如MDN中所述 所述,某些JavaScript是不是JSON,有些JSON不是JavaScript.

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出现了( JSON格式在 RFC 4627 (道格拉斯·克罗克福德(Douglas Crockford)),它已成为首选格式,因为它更轻巧

An example of where this is used is web services responses. In the 'old' days, web services used XML as their primary data format for transmitting back data, but since JSON appeared (The JSON format is specified in RFC 4627 by Douglas Crockford), it has been the preferred format because it is much more lightweight

您可以在官方 JSON网站上找到更多信息.

You can find a lot more info on the official JSON web site.

JSON建立在两个结构上:

JSON is built on two structures:

  • 名称/值对的集合.在各种语言中,这都是作为对象,记录,结构,字典,哈希表,键列表或关联数组实现的.
  • 值的有序列表.在大多数语言中,这是通过数组,向量,列表或序列来实现的.

























以下是JSON数据的示例:

Here is an example of JSON data:

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


JavaScript中的JSON

JSON(使用Javascript)是一个字符串!


JSON in JavaScript

JSON (in Javascript) is a string!

人们经常假定所有Javascript对象都是JSON,而JSON是Javascript对象.这是不正确的.

People often assume all Javascript objects are JSON and that JSON is a Javascript object. This is incorrect.

在Javascript var x = {x:y}中,不是JSON ,这是 Javascript对象.两者不是一回事.等效的JSON(以Javascript语言表示)为var x = '{"x":"y"}'. x string 类型的对象,它本身不是对象.要将其转换为完整的Javascript对象,您必须首先对其进行解析,var x = JSON.parse('{"x":"y"}');x现在是一个对象,但这不再是JSON.

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.

请参见 JavaScript对象与JSON

在使用JSON和JavaScript时,您可能会倾向于使用eval函数来评估回调中返回的结果,但是不建议这样做,因为有两个字符(U + 2028和U + 2029)在JSON中有效,但在JavaScript中无效(此).

When working with JSON and JavaScript, you may be tempted to use the eval function to evaluate the result returned in the callback, but this is not suggested since there are two characters (U+2028 & U+2029) valid in JSON but not in JavaScript (read more of this here).

因此,必须始终尝试使用Crockford的脚本来评估有效的JSON,然后再对其进行评估.可以在此处找到指向脚本说明的链接,这里是直接链接到js文件.如今,每个主要的浏览器都有为此实现的自己的实现.

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)的示例:

Example on how to use the JSON parser (with the json from the above code snippet):

//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'
};

JSON解析器还提供了另一个非常有用的方法stringify.此方法接受JavaScript对象作为参数,并输出回JSON格式的字符串.当您要将数据发送回服务器时,这很有用:

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}

以上两种方法(parsestringify)也采用了第二个参数,该函数将在最终结果的每个级别上为每个键和值调用,每个值都将替换为您输入的功能的结果. (有关更多信息,请参见此处)

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的人,请查看此帖子进行解释并以其他方式确认.

Btw, for all of you out there who think JSON is just for JavaScript, check out this post that explains and confirms otherwise.

  • JSON.org
  • Wikipedia
  • Json in 3 minutes (Thanks mson)
  • Using JSON with Yahoo! Web Services (Thanks gljivar)
  • JSON to CSV Converter
  • Alternative JSON to CSV Converter
  • JSON Lint (JSON validator)

这篇关于什么是JSON,我为什么要使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 01:16
查看更多