如何将 JSON 文件用作数组 [重复]

Posted

技术标签:

【中文标题】如何将 JSON 文件用作数组 [重复]【英文标题】:How do I use an JSON file as an aray [duplicate] 【发布时间】:2021-01-23 22:07:03 【问题描述】:

我正在尝试使用 json 文件作为 arya 使用,所以它的行数不会太长。

Python 中的代码我正在使用 atm 尝试读取 JSON 内容:(我在另一个问题中发现了一些其他内容,它让我走到了这一步)

import json

with open('rooms.json') as f:
    rooms = json.load(f)

rooms.json 中的代码:

[
// The Skeld 0-13
"Upper Engine", 
"Cafeteria", 
"Weapons",
"Reactor",
"Security",
"Medbay",
"O2",
"Navigation",
"Lower Engine",
"Electrical",
"Storage",
"Admin",
"Communications",
"Shields",

// Polus 14-29
"Northwest",
"Northeast",
"Laboratory",
"Security",
"Electrical",
"Storage",
"O2",
"Communications",
"Central",
"Office",
"East",
"Southwest",
"Weapons",
"South",
"Admin",
"Specimen Room",

//MIRA HQ 29-41
"Greenhouse",
"Office",
"Admin",
"Reactor",
"Laboratory",
"launchpad",
"Locker room",
"Communicatins",
"Medbay",
"torage",
"afeteria'"
"balcony"
]

我得到的错误(我使用 sublime btw 和 python 3.8.6):

Traceback (most recent call last):
  File "C:\src\Python\AmongUs\AmongUs.py", line 7, in <module>
    rooms = json.load(f)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2)
[Finished in 0.8s]

Python 中事物的图像:

【问题讨论】:

很遗憾,您的 rooms.json 文件不是 json。你从哪里得到这个文件的?你能说服曾经制作它的人产生有效的json吗? cmets 中的名称是否用于对它们下面的列表进行分组? @buran - 这可能是一个很好的链接,但这取决于 cmets 中的信息是否是 OP 想要保留的内容。可能是我们有 The Skeld 的数据,然后是 Polus 等的数据……OP 似乎不想澄清这一点,所以我们只能等待。 @tdelaney,同意。虽然,如果该信息很重要,那么它真的不应该是评论 当我删除评论我仍然得到错误(也改变了一些东西)pastebin.com/GE7JmzbP 【参考方案1】:

问题是JSON不支持cmets

$ python -c 'import json; json.loads("""[
> // here is a comment
> "some string"]""")'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/michaelsmith/.pyenv/versions/3.8.3/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/Users/michaelsmith/.pyenv/versions/3.8.3/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Users/michaelsmith/.pyenv/versions/3.8.3/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2)

您要么必须删除 cmets 并生成有效的 json,要么使用支持 cmets 的格式。由于 YAML 是 json 的超集,因此您可以将这些文件解析为 yaml,只需将 cmets 更改为以 # 而不是 // 开头。

支持 cmets 的其他格式有 ini(通过 python 的 configparser 模块)和 toml。

【讨论】:

我删除了 cmets 但我收到了这个错误 kojiro pastebin.com/BsbxQAs6 @Martin 在我看来那里的错误很清楚。 我把它修好了 asdasdasdasdasdasdasdasd【参考方案2】:

您可以使用 commentjson 模块来解析带有嵌入式 cmets 的 json。

pip install commentjson

注意:: 你的字符串包含无效的 json 即使你忽略了 cmets! 最后:

“存储”, "食堂'", “阳台” ]

“afeteria”中有一个额外的撇号未转义,并且同一行末尾缺少逗号。

import commentjson as jsonc

jsonc_string = """[
// The Skeld 0-13
"Upper Engine", 
"Cafeteria", 
"Weapons",
"Reactor",
"Security",
"Medbay",
"O2",
"Navigation",
"Lower Engine",
"Electrical",
"Storage",
"Admin",
"Communications",
"Shields",

// Polus 14-29
"Northwest",
"Northeast",
"Laboratory",
"Security",
"Electrical",
"Storage",
"O2",
"Communications",
"Central",
"Office",
"East",
"Southwest",
"Weapons",
"South",
"Admin",
"Specimen Room",

//MIRA HQ 29-41
"Greenhouse",
"Office",
"Admin",
"Reactor",
"Laboratory",
"launchpad",
"Locker room",
"Communicatins",
"Medbay",
"torage",
"Cafeteria",
"balcony"
]"""

lst = jsonc.loads(jsonc_string)

这给了我们解析后的列表:

print(lst)
# Output: 
# ['Upper Engine', 'Cafeteria', 'Weapons', 'Reactor', 'Security', 'Medbay', 'O2', 'Navigation', 'Lower Engine', 'Electrical', 'Storage', 'Admin', 'Communications', 'Shields', 'Northwest', 'Northeast', 'Laboratory', 'Security', 'Electrical', 'Storage', 'O2', 'Communications', 'Central', 'Office', 'East', 'Southwest', 'Weapons', 'South', 'Admin', 'Specimen Room', 'Greenhouse', 'Office', 'Admin', 'Reactor', 'Laboratory', 'launchpad', 'Locker room', 'Communicatins', 'Medbay', 'torage', 'Cafeteria', 'balcony']

【讨论】:

如果你要使用第三方模块在json中启用cmets,为什么不使用yaml? @kojiro 因为 yaml 也将 cmets 作为列表的元素读入。

以上是关于如何将 JSON 文件用作数组 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何在 JSON 文件中导出对象数组,然后使用 es6 导入 [重复]

如何使用 jquery/javascript 将文本插入 json [重复]

PHP错误:不能将stdClass类型的对象用作数组(数组和对象问题)[重复]

如何将 json 对象键转换为不同的数组来删除重复项

如何将对象的json数组反序列化为字典[重复]

将char数组转换为字符串并将其用作标签文本[重复]