Javascript 对象与 JSON [英] Javascript object Vs JSON

查看:25
本文介绍了Javascript 对象与 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想清楚地了解 Javascript 对象和 JSON 字符串之间的基本区别.

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

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

第一季度.键/属性名称是否在带/不带引号都有效?(例如 "one" : 1)

如果是,有什么区别?

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

我觉得他们几乎一样.请详细说明.

Q3:解析 JSON 字符串,是否推荐以下方法?

var javascriptObj = JSON.parse(jSonString);

解决方案

  1. <块引用>

    键/属性名称是否在带/不带引号时都有效?

在使用对象文字表示法时,唯一需要将键括在引号中的情况是键包含特殊字符(if:- 等).值得注意的是,JSON 中的键必须引号括起来.

  1. <块引用>

    如果我使用 var jSonString = JSON.stringify(testObject); 将上述对象转换为 JSON,那么这两个(JS obj 和 JSON)有什么区别?

JSON 是一种数据交换格式.这是一个标准,描述了如何在字符串中表示有序列表和无序映射、字符串、布尔值和数字.就像 XML 和 YAML 是一种在语言之间传递结构化信息的方式一样,JSON 也是如此.另一方面,JavaScript 对象是一种物理类型.就像 PHP 数组、C++ 类/结构体一样,JavaScript 对象也是 JavaScript 的内部类型.

这是一个故事.假设您从商店购买了一些家具,并希望将其交付.然而,库存中仅存的是显示器型号,但您同意购买.

在商店里,你购买的五斗橱是一个活物:

 var chestOfDrawers = {颜色:红色",抽屉数:4}

但是,您不能在邮政中发送一个抽屉柜,因此您将其拆除(阅读,将其串接起来).它现在在家具方面毫无用处.现在是 JSON.它是扁平包装形式.

 {"color":"red","numberOfDrawers":4}

当你收到它时,你会重建抽屉(阅读,解析它).它现在恢复为对象形式.

JSON、XML 和 YAML 背后的原因是使数据能够以参与语言都能理解的格式在编程语言之间传输;你不能直接给 PHP 或 C++ 你的 JavaScript 对象;因为每种语言在幕后都以不同的方式表示一个对象.但是,因为我们已将对象字符串化为 JSON 表示法;即表示数据的标准化方式,我们可以将对象的 JSON 表示传输到另一种语言(C++、PHP),他们可以重新创建我们拥有的 JavaScript 对象到他们的自己的对象基于对象的 JSON 表示.

需要注意的是,JSON 不能表示函数或日期.如果您尝试使用函数成员对对象进行字符串化,则 JSON 表示将省略该函数.日期将被转换为字符串;

 JSON.stringify({foo: 新日期(),等等:函数(){警报('你好');}});//返回字符串 "{"foo":"2011-11-28T10:21:33.939Z"}";

  1. <块引用>

    解析 JSON 字符串,是否推荐使用以下方法?var javascriptObj = JSON.parse(jSonString);

是的,但是较旧的浏览器本身不支持 JSON (IE <8).为了支持这些,您应该包括 json2.js.>

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

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

Let's say I create the following JS variable:

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

Q1. Is the key/property name valid both with/without quotes? (e.g. "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?

I feel they are almost the same. Please elaborate on this.

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

var javascriptObj = JSON.parse(jSonString);

解决方案

  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). It is worth noting that a key in JSON must be enclosed in double quotes.

  1. 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 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 a 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
    }

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 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; 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 language (C++, PHP), they can recreate the JavaScript object we had into their own object based on the JSON representation of the object.

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"}"

  1. For parsing a JSON string, is the method below recommended? var javascriptObj = JSON.parse(jSonString);

Yes, but older browsers don't support JSON natively (IE <8). To support these, you should include 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.

这篇关于Javascript 对象与 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆