【发布时间】:2020-02-07 21:24:09
【问题描述】:
假设我有这个 JSON 字符串(复制自 digitalocean.com)
{
"sammy" : {
"username" : "SammyShark",
"location" : "Indian Ocean",
"online" : true,
"followers" : 987
},
"jesse" : {
"username" : "JesseOctopus",
"location" : "Pacific Ocean",
"online" : false,
"followers" : 432
},
"drew" : {
"username" : "DrewSquid",
"location" : "Atlantic Ocean",
"online" : false,
"followers" : 321
},
"jamie" : {
"username" : "JamieMantisShrimp",
"location" : "Pacific Ocean",
"online" : true,
"followers" : 654
}
}
我需要选择性地仅读取在名为 location 的键中包含 Pacific 的对象/记录。我如何不读取其他记录,例如以“印度洋”和“大西洋”作为位置值的记录?到目前为止,我可以在整个文件中找到所有示例。
非常感谢您的帮助,即使答案是“您只需阅读全文”!
【问题讨论】:
-
为什么你这么反对阅读整篇文章然后过滤它以删除那些错误的海洋?以块的形式读取 JSON 文件之类的内容要困难得多,因为您无法确定它的结构。
标签: json python-3.x