Javascript 对象和 JSON 对象有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6489783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between Javascript Object and JSON object
提问by Refti
Can anyone tell me the difference between Javascript Object and JSON object with an example?
谁能通过一个例子告诉我 Javascript 对象和 JSON 对象之间的区别?
回答by David Tang
A Javascript objectis a data type in Javascript - it makes sense only in Javascript. Often you see a Javascript object literallike this:
Javascript对象是Javascript中的一种数据类型 - 只有在 Javascript 中才有意义。通常你会看到这样的 Javascript对象字面量:
var obj = {
a: 1,
b: 2
};
A JSON stringis 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.
JSON字符串是一种数据交换格式——它只不过是一组以特定方式格式化的字符(为了不同的程序相互通信)。因此,它可以存在于 Javascript 中,也可以存在于另一种语言中,或者只是存储在数据库或文本文件中。
The above Javascript object can be represented in the JSON format in Javascript like this:
上面的 Javascript 对象可以在 Javascript 中以 JSON 格式表示,如下所示:
var json = '{ "a": 1, "b": 2 }';
Or in C# like this:
或者像这样在 C# 中:
string json = "{ \"a\": 1, \"b\": 2 }";
As you can see, a JSON is simply stored inside a string. To make it useful, the JSON string can be parsedto produce an object in any language. Because the JSON format mimics Javascript's object literal syntax, Javascript makes the parsing process easy:
如您所见,JSON 只是存储在字符串中。为了使其有用,可以解析JSON 字符串以生成任何语言的对象。因为 JSON 格式模仿了 Javascript 的对象字面量语法,Javascript 使解析过程变得简单:
var obj = eval('(' + json + ')');
Though typically you'd see:
虽然通常你会看到:
var obj = JSON.parse(json); // for security reasons
Note that JSON is limited in that it cannot store functions - the only values it can contain are:
请注意,JSON 的局限性在于它不能存储函数——它可以包含的唯一值是:
- objects (literals)
- arrays
- numbers
- booleans
- strings
- nulls
- 对象(文字)
- 数组
- 数字
- 布尔值
- 字符串
- 空值
回答by RobG
JSONis a text representation of a javscript object. It is effectively an object literal in javascript notation (hence the name - JavaScript Object Notation => JSON).
JSON是一个 javscript 对象的文本表示。它实际上是 javascript 符号中的对象字面量(因此得名 - JavaScript 对象符号 => JSON)。
If you want to "compare" two object, convert the text to objects then compare keys and values.
如果要“比较”两个对象,请将文本转换为对象,然后比较键和值。
Some examples of objects to/from text:
对象到/从文本的一些示例:
// Create obj using an object literal
var obj = {key: 'value'};
// Convert to text using JSON.stringify
var text = JSON.stringify(obj);
// Show the value of text
alert( text ); // {"key":"value"}
// Create a new object from text
var newObj = JSON.parse(text); // javascript object
// Show the text version of newObj
alert(JSON.stringify(newObj)); // {"key":"value"}
// Use text as code
var newObj2 = eval('(' + text + ')');
// It is indeed a string literal
alert(JSON.stringify(newObj2)); // {"key":"value"}
If you want to compare two objects, convert them from JSON to objects (if they are JSON in the first place) then do something like:
如果要比较两个对象,请将它们从 JSON 转换为对象(如果它们首先是 JSON),然后执行以下操作:
function compareObjects(a, b) {
var i, p, aProps = [], bProps = [];
// Simple test first
if (a === b) {
return true;
}
// Get properties of a
for (p in a) {
if (a.hasOwnProperty(p)) {
aProps.push(p);
}
}
// Get properties of b
for (p in b ) {
if (b.hasOwnProperty(p)) {
bProps.push(p);
}
}
// If don't have same properties, return false
if (aProps.sort().join('') != bProps.sort().join('')) {
return false;
}
// If property values aren't the same, return false
i = aProps.length;
while (i--) {
if (a[aProps[i]] !== b[bProps[i]]) {
return false;
}
}
// If passed all tests, must be equal
return true;
}
回答by Wim ten Brink
JSONstands for "JavaScript Object Notation". Basically, JSON is Javascript, but limited to just filling an object with data. By executing a JSON object, you "load" the data in memory.
JavaScript is the bigger picture, with additional lines of code to manipulate the object or to do all kinds of other stuff.
JSON代表“JavaScript 对象表示法”。基本上,JSON 是 Javascript,但仅限于用数据填充对象。通过执行 JSON 对象,您可以在内存中“加载”数据。
JavaScript 是一个更大的图景,有额外的代码行来操作对象或做各种其他的事情。
A JSON example would be this:
一个 JSON 示例是这样的:
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
A JavaScript example would be this:
JavaScript 示例如下:
var Glossary = {
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
Notice the var Glossary =in the JavaScript?
注意到JavaScript 中的var Glossary =了吗?
回答by Jha
Well First of all a JavaScript is just like any other Object in object oriented programming.
首先,JavaScript 就像面向对象编程中的任何其他对象一样。
And as RobG said JSON is effectively an object literal in Javascript Notation. But Not exactly. According to a Javascript book it says this is an object defined by using Object Notation:
正如 RobG 所说,JSON 实际上是 Javascript Notation 中的对象字面量。但不完全是。根据一本 Javascript 书,它说这是一个使用对象表示法定义的对象:
var newObject =
{ prop1 : true,
showMessage : function (msg) {alert(msg)}
};
According to JSON in JavaScript,
根据 JavaScript 中的 JSON,
JSON is a subset of the object literal notation of JavaScript.
JSON 是 JavaScript 的对象字面量表示法的一个子集。
Also you might want to consider taking a look this link
你也可以考虑看看这个链接
回答by developer747
var object = {
name: "John",
profession: "blogger"
};
alert(object.name);//John
alert(typeof(object));//Object
alert(object);//[object Object]
var json = JSON.stringify(object);
alert(json.name);//undefined
alert(typeof(json));//string
alert(json);//{"name":"John","profession":"blogger"}