Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

javascript - Javascript对象与JSON(Javascript object Vs JSON)

I want to understand the basic differences clearly between Javascript object and JSON string.

(我想明白Javascript对象和JSON字符串之间的基本差异。)

Let's say I create the following JS variable:

(假设我创建了以下JS变量:)

var testObject = {one: 1,"two":2,"three":3};

Q1.

(Q1。)

Is the key/property name valid both with/without quotes?

(键/属性名称是否有/无引号?)

(eg "one" : 1 )

((例如"one" : 1 ))

If yes, what is the difference?

(如果是,有什么区别?)

Q2: If I convert the above object using JSON.stringify(testObject) , what's the difference between the original JS object and the JSON?

(Q2:如果我使用JSON.stringify(testObject)转换上述对象,原始JS对象和JSON之间有什么区别?)

I feel they are almost the same.

(我觉得他们差不多了。)

Please elaborate on this.

(请详细说明。)

Q3: For parsing a JSON string, is the method below recommended?

(问题3:对于解析JSON字符串,是推荐的方法吗?)

var javascriptObj = JSON.parse(jSonString);
  ask by testndtv translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Is the key/property name valid both with/without quotes ?

    (键/属性名称是否有/无引号?)

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

    (在使用Object Literal表示法时,您需要将键括在引号中的唯一时间是键包含特殊字符( if:- etc)。)

    It is worth noting that a key in JSON must be enclosed in double quotes.

    (值得注意的是,JSON中的键必须引号括起来。)

  2. If I convert the above object to JSON using var jSonString = JSON.stringify(testObject);

    (如果我使用var jSonString = JSON.stringify(testObject);将上述对象转换为JSON var jSonString = JSON.stringify(testObject);)

    , what is the difference between the 2 (JS obj and JSON)?

    (,2(JS obj和JSON)有什么区别?)

    JSON is a data interchange format.

    (JSON是一种数据交换格式。)

    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.

    (就像XML和YAML是一种在语言之间传递结构化信息的方式一样,JSON是相同的。)

    A JavaScript object on the other hand is a physical type.

    (另一方面,JavaScript对象是物理类型。)

    Just like a PHP array, a C++ class/ struct, a JavaScript object is an type internal to JavaScript.

    (就像PHP数组,C ++类/结构一样,JavaScript对象是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 } 

    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.

    (它现在是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.

    (它现在回到了一个对象形式。)

    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;

    (JSON / XML和YAML背后的原因是使数据能够以参与语言可以理解的格式在编程语言之间传输;)

    you can't give PHP or C++ your JavaScript object directly;

    (你不能直接给PHP或C ++你的JavaScript对象;)

    because each language represents an object differently under-the-hood.

    (因为每种语言都表示不同的对象。)

    However, because we've stringified the object into JSON notation;

    (但是,因为我们已经将对象字符串化为JSON表示法;)

    ie 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 表示传递给另一个语言(C ++,PHP),它们可以根据对象的JSON表示重新创建我们拥有的JavaScript对象。)

    It is important to note that JSON cannot represent functions or dates.

    (请务必注意,JSON不能表示函数或日期。)

    If you attempt to stringify an object with a function member, the function will be omitted from the JSON representation.

    (如果尝试使用函数成员对对象进行字符串化,则将从JSON表示中省略该函数。)

    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"}" 
  3. For parsing a JSON string, is the method below recommended?

    (对于解析JSON字符串,是推荐的方法吗?)

    var javascriptObj = JSON.parse(jSonString);

    Yes, but older browsers don't support JSON natively (IE <8) .

    (是的,但是较旧的浏览器本身不支持JSON(IE <8) 。)

    To support these, you should include json2.js .

    (要支持这些,您应该包含json2.js 。)

    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.

    (如果您正在使用jQuery,则可以调用jQuery.parseJSON() ,如果支持它将使用JSON.parse() ,否则将回退到自定义实现来解析输入。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...