如何使用Python迭代JSON命令
我的密码是如何使用Python迭代JSON命令,python,json,python-3.x,loops,parsing,Python,Json,Python 3.x,Loops,Parsing,我的密码是 with open('base.json') as f: data = json.load(f) for Object in data["Objects"][0]: print(Object["Name"]) 我正在努力打印 SellMenu BuyMenu 因此,我可以添加按钮到特定的菜单,但这段代码不工作,它崩溃 我的JSON(base.JSON)是 试试这个: for Objec
with open('base.json') as f:
data = json.load(f)
for Object in data["Objects"][0]:
print(Object["Name"])
我正在努力打印
SellMenu
BuyMenu
因此,我可以添加按钮到特定的菜单,但这段代码不工作,它崩溃
我的JSON(base.JSON)是
试试这个:
for Object in data["Objects"]:
print(Object["Name"])
说明:
通过对数据中的对象[“对象”][0]:执行此操作,实际上您正在尝试迭代列表中的第一个元素。
为了迭代列表本身,您必须为数据[“对象”]中的对象编写
:
。实际上,data[“Objects”]
的类型是list。使用与您发布的test.json
文件中相同的内容,此函数当前按要求执行。现在我只想指出,Object
是python中的一个保留关键字,这可能会在这里引起冲突:
def jsonTest():
import json
with open('test.json', 'r') as f:
data = json.load(f)
for element in data['Objects']:
print(element['Name'])
jsonTest()
输出:
> python .\testing.py
SellMenu
BuyMenu
> python .\testing.py
Element: Type <==> Type: <class 'str'>
Element: Path <==> Type: <class 'str'>
Element: Name <==> Type: <class 'str'>
Element: X <==> Type: <class 'str'>
Element: Y <==> Type: <class 'str'>
Element: Width <==> Type: <class 'str'>
Element: Height <==> Type: <class 'str'>
Element: Buttons <==> Type: <class 'str'>
Element: Text <==> Type: <class 'str'>
> python .\testing.py
Type: <class 'dict'>
Dict: {'Type': 'Menu', 'Path': 'insert_path', 'Name': 'SellMenu', 'X': 0, 'Y': 0, 'Width': 1920, 'Height': 1080, 'Buttons': [], 'Text': []}
Type: <class 'dict'>
Dict: {'Type': 'Menu', 'Path': 'insert_path', 'Name': 'BuyMenu', 'X': 0, 'Y': 0, 'Width': 1920, 'Height': 1080, 'Buttons': [], 'Text': []}
>>> d = {'Type': 'Menu', 'Path': 'insert_path', 'Name': 'SellMenu', 'X': 0, 'Y': 0, 'Width': 1920, 'Height': 1080, 'Buttons': [], 'Text': []}
>>> d['Buttons']
[]
>>> d['Buttons'] = {"RX": 123, "RY": 321, "Width": 10, "Height" : 73, "Name" : 'Robin'}
>>> d['Buttons']
{'RX': 123, 'RY': 321, 'Width': 10, 'Height': 73, 'Name': 'Robin'}
通过使用数据['Objects'][0]列表的第0个索引,您要求迭代器循环以下内容:
for element in data['Objects'][0]:
print(f'Element: {element:8s} <==> Type: {type(element)}')
因此,您需要迭代数据['Objects']
列表
for element in data['Objects']:
print(f'Type: {type(element)}\nDict: {element}')
输出:
> python .\testing.py
SellMenu
BuyMenu
> python .\testing.py
Element: Type <==> Type: <class 'str'>
Element: Path <==> Type: <class 'str'>
Element: Name <==> Type: <class 'str'>
Element: X <==> Type: <class 'str'>
Element: Y <==> Type: <class 'str'>
Element: Width <==> Type: <class 'str'>
Element: Height <==> Type: <class 'str'>
Element: Buttons <==> Type: <class 'str'>
Element: Text <==> Type: <class 'str'>
> python .\testing.py
Type: <class 'dict'>
Dict: {'Type': 'Menu', 'Path': 'insert_path', 'Name': 'SellMenu', 'X': 0, 'Y': 0, 'Width': 1920, 'Height': 1080, 'Buttons': [], 'Text': []}
Type: <class 'dict'>
Dict: {'Type': 'Menu', 'Path': 'insert_path', 'Name': 'BuyMenu', 'X': 0, 'Y': 0, 'Width': 1920, 'Height': 1080, 'Buttons': [], 'Text': []}
>>> d = {'Type': 'Menu', 'Path': 'insert_path', 'Name': 'SellMenu', 'X': 0, 'Y': 0, 'Width': 1920, 'Height': 1080, 'Buttons': [], 'Text': []}
>>> d['Buttons']
[]
>>> d['Buttons'] = {"RX": 123, "RY": 321, "Width": 10, "Height" : 73, "Name" : 'Robin'}
>>> d['Buttons']
{'RX': 123, 'RY': 321, 'Width': 10, 'Height': 73, 'Name': 'Robin'}
这里的问题是默认情况下,['Buttons']是一个列表[]
而不是一个字典{}
,因此使用动态键入,您可以通过将其分配给新类型(=
)来重新键入(重新声明类型):
输出:
> python .\testing.py
SellMenu
BuyMenu
> python .\testing.py
Element: Type <==> Type: <class 'str'>
Element: Path <==> Type: <class 'str'>
Element: Name <==> Type: <class 'str'>
Element: X <==> Type: <class 'str'>
Element: Y <==> Type: <class 'str'>
Element: Width <==> Type: <class 'str'>
Element: Height <==> Type: <class 'str'>
Element: Buttons <==> Type: <class 'str'>
Element: Text <==> Type: <class 'str'>
> python .\testing.py
Type: <class 'dict'>
Dict: {'Type': 'Menu', 'Path': 'insert_path', 'Name': 'SellMenu', 'X': 0, 'Y': 0, 'Width': 1920, 'Height': 1080, 'Buttons': [], 'Text': []}
Type: <class 'dict'>
Dict: {'Type': 'Menu', 'Path': 'insert_path', 'Name': 'BuyMenu', 'X': 0, 'Y': 0, 'Width': 1920, 'Height': 1080, 'Buttons': [], 'Text': []}
>>> d = {'Type': 'Menu', 'Path': 'insert_path', 'Name': 'SellMenu', 'X': 0, 'Y': 0, 'Width': 1920, 'Height': 1080, 'Buttons': [], 'Text': []}
>>> d['Buttons']
[]
>>> d['Buttons'] = {"RX": 123, "RY": 321, "Width": 10, "Height" : 73, "Name" : 'Robin'}
>>> d['Buttons']
{'RX': 123, 'RY': 321, 'Width': 10, 'Height': 73, 'Name': 'Robin'}
总是乐于助人 @KyleKip你确定吗?它对我有用。您得到的结果/错误是什么?@ThunderPhenix如果我试图使用
数据[“Objects”][0][“Buttons”]添加到第二个字典中。append
我该怎么做?现在它只是增加了第一个one@KyleKip我不明白你的问题。请尽量明确。好的,我有两个菜单,一个是SellMenu
和BuyMenu
都有一个Buttons[]
,在我的python代码中,我用这行代码附加到JSON文件data[“Objects”][0][“Buttons”]。附加({“RX”:ButtonObject.x,“RY”:ButtonObject.y,“Width”:ButtonObject.Width,“Height”:ButtonObject.Height,“Path”:ButtonObject.path,“Name”:ButtonObject.Name})
当我调用它时,它会添加到第一个按钮[]
中,它会找到不正确的按钮。如何指定要使用哪一个。不知道如何告诉它进入BuyMenu
或SellMenu
@KyleKip使用data[“Objects”][0]…
用于SellMenu
和data[“Objects”][1]…
用于BuyMenu
从data[“Objects”][0]
中删除,噢,这很有意义,谢谢!快速提问。如果我想添加类似于data[“Objects”][0][“Buttons”].append({“RX”:ButtonObject.x,“RY”:ButtonObject.y,“Width”:ButtonObject.Width,“Height”:ButtonObject.Height,“Name”:ButtonObject.Name})
这是SellMenu还是BuyMenu,我怎么能明确地这么说呢?