javascript python对象和json对象有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5584943/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:41:07  来源:igfitidea点击:

what's the difference between python objects and json objects?

javascriptpythondjangojsonnode.js

提问by Stephen

On the surface it appears that python uses json natively. The only exception I can think of is the fact that json can store js functions.

从表面上看,python 似乎本机使用 json。我能想到的唯一例外是json可以存储js函数。

Here's my issue: I need to pass json to a python file through the terminal.
Why should or shouldn't I just use eval()?

这是我的问题:我需要通过终端将 json 传递给 python 文件。
为什么我应该或不应该只使用 eval()?

回答by brandizzi

No, Python does not use JSON natively. This stuff you think is JSON is, in fact, a dictionary, oneof many kinds of objects in Python. The (easy) syntax for building a dictionary in Python is pretty close to JSON but it is incidental. As you can create a dictionary this way:

不,Python 本身不使用 JSON。这个你认为是 JSON 的东西,实际上是一个字典,Python 中多种对象之一。在 Python 中构建字典的(简单)语法与 JSON 非常接近,但它是偶然的。因为您可以通过这种方式创建字典:

a = {'a' : 2, 'b' : 3}

you can create it this way, too:

你也可以这样创建:

a = dict([('a', 2), ('b', 3)]);

So, what are the syntaxes so similar? Well, JSON syntax is inspiredby JavaScript syntax for arrays. It is likely that the JavaScript syntax also inspiredthe way Python dictionaries are written or vice versa. But never assumes these three syntaxes – JavaScript, JSON and Python dicts - to be the same or interchangeable.

那么,什么是语法如此相似?好吧,JSON 语法的灵感来自于数组的 JavaScript 语法。很可能 JavaScript 语法也启发了 Python 字典的编写方式,反之亦然。但永远不要假设这三种语法——JavaScript、JSON 和 Python dicts——是相同的或可以互换的。

Given that, why should you not use eval()for convert JSON in a dictionary? Firstly, because eval()can do anything in Python – such as exiting the program, removing a file, changing some internal data etc. etc. Hence, by using eval(), you might make yourself vulnerable to code injection, depending on how you use it. Also, using eval()for converting JSON to a dict assumes the syntax of both are identical – which is not necessarily true; even if the syntaxes were identical, they cannot be in the future. Finally, there is a much better and more practical way to parse JSON: the jsonmodule:

鉴于此,为什么不应该eval()在字典中使用转换 JSON?首先,因为eval()可以在 Python 中做任何事情——例如退出程序、删除文件、更改一些内部数据等。因此,通过使用eval(),您可能会使自己容易受到代码注入的影响,这取决于您如何使用它。此外,eval()用于将 JSON 转换为 dict 假定两者的语法是相同的——这不一定是正确的;即使语法相同,将来也不可能如此。最后,还有一种更好、更实用的方式来解析 JSON:json模块:

>>> import json
>>> json.loads('{"a":1}')
{'a': 1}

Use it to parse your JSON.

使用它来解析您的 JSON。

Good luck!

祝你好运!

回答by Ignacio Vazquez-Abrams

JSON does not have objects per se, and cannot store JavaScript functions. Its syntax may appear similar to JavaScript literals, but trying to use it as such all the time will cause nothing but pain.

JSON 本身没有对象,也不能存储 JavaScript 函数。它的语法可能看起来类似于 JavaScript 文字,但尝试一直这样使用它只会带来痛苦。

And there should be no need to use eval(); both JavaScript and Python have JSON parsers and serializers readily available.

并且应该没有必要使用eval(); JavaScript 和 Python 都有现成的 JSON 解析器和序列化器。