Javascript对象与JSON [英] Javascript object Vs JSON
问题描述
我想明白Javascript对象和JSON字符串之间的基本差异。
I want to understand the basic differences clearly between Javascript object and JSON string.
假设我创建了以下JS变量:
Let's say I create the following JS variable:
var testObject = {one: 1,"two":2,"three":3};
Q1。密钥/属性名称是否有/无引号?(例如one:1
)
如果是,有什么区别?
Q2:如果我使用转换上述对象JSON.stringify(testObject)
,原始JS对象和JSON之间的区别是什么?
Q2: If I convert the above object using JSON.stringify(testObject)
, what’s the difference between the original JS object and the JSON?
我觉得它们几乎是一样的。请详细说明。
I feel they are almost the same. Please elaborate on this.
问题3:对于解析JSON字符串,是推荐的方法吗?
var javascriptObj = JSON.parse(jSonString);
推荐答案
键/属性名称是否有/无引号?
Is the key/property name valid both with/without quotes ?
您需要附上一个使用Object Literal表示法时键中的键是键包含特殊字符的位置( if
,:
, -
等)。值得注意的是,JSON 中的密钥必须包含在 double 引号中。
The only time you need to enclose a key in quotes when using Object Literal notation is where the key contains a special character (if
, :
, -
etc). It is worth noting that a key in JSON must be enclosed in double quotes.
如果我使用
var jSonString = JSON.stringify(testObject);
将上述对象转换为JSON,则2(JS obj和JSON)之间有什么区别)?
If I convert the above object to JSON using
var jSonString = JSON.stringify(testObject);
, what is the difference between the 2 (JS obj and JSON)?
JSON 是一种数据交换格式。它是一个标准,它描述了有序列表和无序映射,字符串布尔值和数字如何用字符串表示。就像XML和YAML是一种在语言之间传递结构化信息的方式一样,JSON是相同的。另一方面,JavaScript对象是物理类型。就像PHP数组,C ++类/结构一样,JavaScript对象是JavaScript内部的类型。
JSON is a data interchange format. It's a standard which describes how ordered lists and unordered maps, strings booleans and numbers can be represented in a string. Just like XML and YAML is a way to pass structured information between languages, JSON is the same. A JavaScript object on the other hand is a physical type. Just like a PHP array, a C++ class/ struct, a JavaScript object is an type internal to JavaScript.
这是一个故事。让我们想象一下,你从商店购买了一些家具,并希望它能够送到。然而,库存中剩下的唯一一个是显示器型号,但你同意购买它。
Here's a story. Let's imagine you've purchased some furniture from a store, and you want it delivered. However the only one left in stock is the display model, but you agree to buy it.
在商店里,你购买的抽屉柜是一个生活对象:
In the shop, the chest-of-drawers you've purchased is a living object:
var chestOfDrawers = {
color: "red",
numberOfDrawers: 4
}
但是,你不能在帖子中发送抽屉柜,所以你拆除它(阅读,字符串化)。现在它在家具方面毫无用处。它现在是JSON。它是扁平包装形式。
However, you can't send a chest-of-drawers in the post, so you dismantle it (read, stringify it). It's now useless in terms of furniture. It is now JSON. Its in flat pack form.
{"color":"red","numberOfDrawers":4}
当你收到它时,你会重建抽屉柜(阅读,解析它)。它现在以对象形式返回。
When you receive it, you then rebuild the chest-of-drawers (read, parse it). Its now back in an object form.
JSON / XML和YAML背后的原因是使数据能够以参与语言可以理解的格式在编程语言之间传输;你不能直接给PHP或C ++你的JavaScript对象;因为每种语言都表示不同的对象。但是,因为我们已经将对象字符串化为JSON表示法;即表示数据的标准化方法,我们可以将对象的JSON 表示传输到另一个语言(C ++,PHP),它们可以重新创建我们所拥有的JavaScript对象在对象的JSON表示上拥有自己的对象 。
The reason behind JSON/ XML and YAML is to enable data to be transferred between programming languages in a format both participating languages can understand; you can't give PHP or C++ your JavaScript object directly; because each language represents an object differently under-the-hood. However, because we've stringified the object into JSON notation; i.e. a standardised way to represent data, we can transmit the JSON representation of the object to another langauge (C++, PHP), they can recreate the JavaScript object we had into their own object based on the JSON representation of the object.
重要的是要注意JSON不能表示函数或日期。如果尝试使用函数成员对对象进行字符串化,则将从JSON表示中省略该函数。日期将转换为字符串;
It is important to note that JSON cannot represent functions or dates. If you attempt to stringify an object with a function member, the function will be omitted from the JSON representation. A date will be converted to a string;
JSON.stringify({
foo: new Date(),
blah: function () {
alert('hello');
}
}); // returns the string "{"foo":"2011-11-28T10:21:33.939Z"}"
对于解析JSON字符串,是推荐的方法吗?
var javascriptObj = JSON.parse(jSonString);
是的,但< a href =http://caniuse.com/#search=json =noreferrer>旧浏览器本身不支持JSON(IE< 8)。要支持这些,您应该包括 json2.js
一>。
Yes, but older browsers don't support JSON natively (IE <8). To support these, you should include json2.js
.
如果您使用的是jQuery,可以致电 jQuery.parseJSON()
,如果支持,它将在引擎盖下使用 JSON.parse()
否则将回退到自定义实现来解析输入。
If you're using jQuery, you can call jQuery.parseJSON()
, which will use JSON.parse()
under the hood if it's supported and will otherwise fallback to a custom implementation to parse the input.
这篇关于Javascript对象与JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!