列表上的JSON.stringify(Javascript)和json.dumps(Python)不相等吗? [英] JSON.stringify (Javascript) and json.dumps (Python) not equivalent on a list?
本文介绍了列表上的JSON.stringify(Javascript)和json.dumps(Python)不相等吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在javascript中
In javascript:
var myarray = [2, 3];
var json_myarray = JSON.stringify(myarray) // '[2,3]'
但是在Python中:
But in 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.
对此有一些解释吗?
推荐答案
区别在于json.dumps
默认情况下会应用一些次要的漂亮打印,而JSON.stringify
则不会.
The difference is that json.dumps
applies some minor pretty-printing by default but JSON.stringify
does not.
要删除所有空白,例如JSON.stringify
,您需要指定分隔符.
To remove all whitespace, like JSON.stringify
, you need to specify the separators.
json_mylist = json.dumps(mylist, separators=(',', ':'))
这篇关于列表上的JSON.stringify(Javascript)和json.dumps(Python)不相等吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文