JSON.stringify(Javascript)和json.dumps(Python)在列表中不相同?

[英]JSON.stringify (Javascript) and json.dumps (Python) not equivalent on a list?


In javascript:

在javascript中:

var myarray = [2, 3];
var json_myarray = JSON.stringify(myarray) // '[2,3]'

But in Python:

但在Python中:

mylist = [2, 3]
json_mylist = json.dumps(mylist) # '[2, 3]' <-- Note the space

So the 2 functions aren't equivalent. It's a bit unexpected for me and a bit problematic when trying to compare some data for example.

所以这2个功能并不相同。这对我来说有点意外,并且在尝试比较一些数据时有点问题。

Some explanation about it?

关于它的一些解释?

1 个解决方案

#1


11  

The difference is that json.dumps applies some minor pretty-printing by default but JSON.stringify does not.

区别在于json.dumps默认应用了一些小的漂亮打印,但JSON.stringify没有。

To remove all whitespace, like JSON.stringify, you need to specify the separators.

要删除所有空格,例如JSON.stringify,您需要指定分隔符。

json_mylist = json.dumps(mylist, separators=(',',':'))

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2017/09/14/d14ae7937f64ecdf5e912b3e6e93028.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号