python - 读取json、解析成data、json.dumps(data)后json无效
问题描述
考虑python中的以下代码:
import requests
import json
url = 'https://api.pushshift.io/reddit/submission/search?ids=d1de8g'
raw_data = requests.get(url)
data = raw_data.json()['data'][0]
js = json.dumps(data)
js
不返回有效的 json。
(这个json可以在这里看到)
任何 web 解析器都会报告 key 的问题selftext
。
Elasticsearch 不会接受这个 json。
也就是说,json.loads(js)
工作正常并返回正确的对象。
我怀疑问题在于 json.dumps 转义引号,例如在C\'est
. 但我不确定如何处理。
理想情况下,我想尽可能多地限制data
.
解决方案
我无法重现您的问题。我刚刚在您的代码末尾添加了这个:
with open('temp.json', 'w') as f:
f.write(js)
我打开该文件并使用在线 json lint 对其进行测试,它是完全有效的。
这是完整的文件内容:
{
"contest_mode": false,
"wls": 6,
"send_replies": true,
"updated_utc": 1568047643,
"author_flair_type": "text",
"domain": "self.france",
"allow_live_comments": false,
"subreddit": "france",
"is_original_content": false,
"whitelist_status": "all_ads",
"author_fullname": "t2_7u9qc9o",
"link_flair_background_color": "",
"is_reddit_media_domain": false,
"id": "d1de8g",
"is_robot_indexable": true,
"is_meta": false,
"no_follow": true,
"author": "Hideki__",
"num_crossposts": 0,
"num_comments": 18,
"can_mod_post": false,
"link_flair_richtext": [],
"is_crosspostable": true,
"pinned": false,
"score": 13,
"retrieved_on": 1567961258,
"over_18": false,
"pwls": 6,
"all_awardings": [],
"subreddit_id": "t5_2qhjz",
"media_only": false,
"thumbnail_height": 105,
"steward_reports": [],
"author_flair_richtext": [],
"author_patreon_flair": false,
"permalink": "/r/france/comments/d1de8g/champignons/",
"gildings": {},
"thumbnail_width": 140,
"parent_whitelist_status": "all_ads",
"is_self": true,
"full_link": "https://www.reddit.com/r/france/comments/d1de8g/champignons/",
"spoiler": false,
"media_metadata": {
"l8yvwzw2fel31": {
"status": "valid",
"m": "image/jpg",
"s": {
"y": 1500,
"x": 2000,
"u": "https://i.redd.it/l8yvwzw2fel31.jpg"
},
"e": "Image",
"id": "l8yvwzw2fel31"
},
"g5ggrww2fel31": {
"status": "valid",
"m": "image/jpg",
"s": {
"y": 1500,
"x": 2000,
"u": "https://i.redd.it/g5ggrww2fel31.jpg"
},
"e": "Image",
"id": "g5ggrww2fel31"
},
"vbk1czx2fel31": {
"status": "valid",
"m": "image/jpg",
"s": {
"y": 1500,
"x": 2000,
"u": "https://i.redd.it/vbk1czx2fel31.jpg"
},
"e": "Image",
"id": "vbk1czx2fel31"
},
"blznmww2fel31": {
"status": "valid",
"m": "image/jpg",
"s": {
"y": 1500,
"x": 2000,
"u": "https://i.redd.it/blznmww2fel31.jpg"
},
"e": "Image",
"id": "blznmww2fel31"
}
},
"locked": false,
"stickied": false,
"total_awards_received": 0,
"url": "https://www.reddit.com/r/france/comments/d1de8g/champignons/",
"link_flair_type": "text",
"subreddit_subscribers": 293573,
"created_utc": 1567961252,
"thumbnail": "https://b.thumbs.redditmedia.com/yS3060tLjUvrvHElg1nUCQwu7YPwKBdjNCXuhD4lqsM.jpg",
"link_flair_text_color": "dark",
"selftext": "Bonjour le sub, je suis enfin en vacances et dans le Livradois en Auvergne. Je cherche les champignons, je les fais s\u00e9cher et aussi du serpolet.\n\nJ'ai des girolles jaunes et grises, des c\u00e8pes, des pieds de moutons et je trouve un nouveau champignons qui je pense \u00eatre un polypore confluent. C'est l\u00e0 que je vous demande votre avis et tout ce que vous pourrez me dire de ce champignons tout nouveau pour moi.\n\nEt enfin la 4eme photos, c'est un champignons que j'ai pas du tout envie de manger mais j'aimerais bien conna\u00eetre son nom.\n\n[est-ce un polypore confluent?](https://i.redd.it/vbk1czx2fel31.jpg)\n\n[est-ce un polypore confluent?](https://i.redd.it/blznmww2fel31.jpg)\n\n[est-ce un polypore confluent?](https://i.redd.it/g5ggrww2fel31.jpg)\n\n​\n\n[Champignon inconnue](https://i.redd.it/l8yvwzw2fel31.jpg)",
"title": "Champignons",
"subreddit_type": "public",
"is_video": false
}
我可以看到你的粘贴有双重转义,我不知道为什么。也许这与您如何打印变量的内容有关。
推荐阅读
- python - 如何解决 toml.decoder.TomlDecodeError: Key group not on a line by itself。(第 1 行第 1 列 ...)调用 streamlit 包时出错?
- assembly - 给定两个内存地址,我可以将数据从一个地址移动到另一个地址,而该数据不会进入寄存器
- python - 是否可以使用 Python csv 阅读器读取使用 C++ std::setw 生成的文件?
- flutter - 在初始连接请求时向 IOWebSocketChannel 发送数据
- java - Apache POI 3.16 在 Matlab 中运行时会创建无效文件
- python - 如何在 Python 中使用 psycopg2-binary?
- postgresql - 使用 CTE 时,最好使用 join 或 in() 进行过滤?
- error-handling - 编译器返回错误的各个部分是什么意思?
- sql - 从包含数组元素的 Presto 列中选择值
- python - 在 BeautifulSoup 中处理无限滚动 UI