添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

转储为JSON增加了额外的双引号和转义引号

124 人关注

我正在用一个Python工具检索Twitter数据,并将这些数据以JSON格式转储到我的磁盘上。我注意到一个意外的转义,即一条推文的整个数据字符串被括在双引号中。此外,实际JSON格式的所有双引号都用反斜杠转义了。

They look like this:

"Fri Aug 08 11:04:40 +0000 2014\",\"id\":497699913925292032,

我怎样才能避免这种情况?应该是这样。

{"created_at": "Fri Aug 08 11:04:40 +0000 2014" .....

My file-out code looks like this:

with io.open('data'+self.timestamp+'.txt', 'a', encoding='utf-8') as f:
            f.write(unicode(json.dumps(data, ensure_ascii=False)))
            f.write(unicode('\n'))

在以后的处理步骤中读入JSON文件时,非故意的转义会导致问题。

python
json
toobee
toobee
发布于 2014-08-11
5 个回答
Martijn Pieters
Martijn Pieters
发布于 2022-03-14
已采纳
0 人赞同

你正在对你的JSON字符串进行双重编码。 data 已经 一个JSON字符串,不需要进行编码 再次 :

>>> import json
>>> not_encoded = {"created_at":"Fri Aug 08 11:04:40 +0000 2014"}
>>> encoded_data = json.dumps(not_encoded)
>>> print encoded_data
{"created_at": "Fri Aug 08 11:04:40 +0000 2014"}
>>> double_encode = json.dumps(encoded_data)
>>> print double_encode
"{\"created_at\": \"Fri Aug 08 11:04:40 +0000 2014\"}"

Just write these directly to your file:

with open('data{}.txt'.format(self.timestamp), 'a') as f:
    f.write(data + '\n')
    
f.write(data + '\n' ) -- 与你的例子中的 -- data = encoded_data -- 相关。
@RichElswick OP使用了变量 data ,其中包含已经编码的JSON数据,所以是的,我使用了变量名称 encoded_data 来说明发生了什么。
对于那些像我一样,在双重escaping时得到 \\" 的人,这是因为在解释器中像 double_encode 这样的裸变量将为你逃避反斜线。如果你使用 print(double_encode) ,就像Martijn使用的那样,双重escaped的字符串将被打印成单反斜线,如图所示。
Mike Maxwell
Mike Maxwell
发布于 2022-03-14
0 人赞同

另一种可能发生这种不需要的转义的情况是,如果你试图对json.dump()的预处理输出使用json.dump()。 比如说

import json, sys
json.dump({"foo": json.dumps([{"bar": 1}, {"baz": 2}])},sys.stdout)

will result in

{"foo": "[{\"bar\": 1}, {\"baz\": 2}]"}

为了避免这种情况,你需要传递字典而不是json.dumps()的输出,例如

json.dump({"foo": [{"bar": 1}, {"baz": 2}]},sys.stdout)

which outputs the desired

{"foo": [{"bar": 1}, {"baz": 2}]}

(你问为什么要用json.dumps()来预处理内部列表? 好吧,我有另一个函数在用其他东西创建内部列表,我认为从该函数返回一个json对象是有意义的......错了)。

TechFree
TechFree
发布于 2022-03-14
0 人赞同

对于有类似问题的人来说,我用这个方法将JSON格式的数据转储到文件中,这些数据来自于一个API调用。下面只是一个指示性的例子,根据你的要求进行更新

import json
# below is an example, this came for me from an API call
json_string = '{"address":{"city":"NY", "country":"USA"}}'
# dump the JSON data into file ( dont use json.dump as explained in other answers )
with open('direct_json.json','w') as direct_json:    
    direct_json.write(json_string)
    direct_json.write("\n")
# load as dict
json_dict = json.loads(json_string)
# pretty print
print(json.dumps(json_dict, indent = 1)) 
# write pretty JSON to file
with open('formatted.json','w') as formatted_file: 
    json.dump(json_dict, formatted_file, indent=4)  
    
Omar Dasser
Omar Dasser
发布于 2022-03-14
0 人赞同

简单的方法是在转储之前使用json loads函数,比如下面的方法,这对我来说很有效。

import json
data = json.loads('{"foo": json.dumps([{"bar": 1}, {"baz": 2}])}')
with open('output.json','w') as f:
   json.dump(data,f,indent=4)
    
AdiSa
AdiSa
发布于 2022-03-14
0 人赞同

设置 escape_forward_slashes=False,以防止转义 / 字符。

Solved:

ujson.dumps({"a":"aa//a/dfdf"}, escape_forward_slashes=False )

'{"a":"aa//a/dfdf"}'

默认情况下。