Javascript对象和JSON对象之间有什么区别 [英] What's the difference between Javascript Object and JSON object

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

问题描述

有人能用一个例子告诉我Javascript对象和JSON对象之间的区别吗?

Can anyone tell me the difference between Javascript Object and JSON object with an example?

推荐答案

一个Javascript 对象是Javascript中的数据类型 - 仅在Javascript中才有意义。通常你会看到这样的Javascript 对象文字

A Javascript object is a data type in Javascript - it makes sense only in Javascript. Often you see a Javascript object literal like this:

var obj = {
    a: 1,
    b: 2
};






JSON 字符串是一种数据交换格式 - 它只不过是以特定方式格式化的一堆字符(为了让不同的程序相互通信)。因此,它可以存在于Javascript中,也可以存在于其他语言中,或者只是存储在数据库或文本文件中。


A JSON string is a data interchange format - it is nothing more than a bunch of characters formatted a particular way (in order for different programs to communicate with each other). Because of this, it can exist inside Javascript, or in another language or simply stored inside a database or a text file.

上述Javascript对象可以用JSON表示Javascript中的格式如下:

The above Javascript object can be represented in the JSON format in Javascript like this:

var json = '{ "a": 1, "b": 2 }';

或者像C#这样:

string json = "{ \"a\": 1, \"b\": 2 }";

如您所见,JSON只是存储在字符串中。为了使它有用,可以解析字符串以生成任何语言的对象。因为JSON格式模仿Javascript的对象文字语法,所以Javascript使解析过程变得简单:

As you can see, a JSON is simply stored inside a string. To make it useful, the JSON string can be parsed to produce an object in any language. Because the JSON format mimics Javascript's object literal syntax, Javascript makes the parsing process easy:

var obj = eval('(' + json + ')');

虽然通常你会看到:

var obj = JSON.parse(json); // for security reasons






注意JSON是有限的因为它不能存储函数 - 它可以包含的唯一值是:


Note that JSON is limited in that it cannot store functions - the only values it can contain are:


  • 对象(文字)

  • 数组

  • 数字

  • 布尔值

  • 字符串

  • nulls

  • objects (literals)
  • arrays
  • numbers
  • booleans
  • strings
  • nulls

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

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