繁体   English   中英

Python和JSON解析。 TypeError:列表索引必须是整数,而不是str

[英]Python & JSON Parsing. TypeError: list indices must be integers, not str

我是python和编程的完全业余爱好者。 我正在尝试从服务器上的JSON数据文件中读取数据列表。 它生成以下错误:

TypeError: list indices must be integers, not str

我认为这可以追溯到需要进行某种更改的声明,但我不知道应该怎么做。

def grabDeviceIDs():
    devices=[];
    ## parse the list
    for deviceListJSON in (getDeviceListFromserver()['mobile_device_group']['mobile_devices']['mobile_device']):
        d = Device()
        d.id = deviceListJSON.get('id')
        devices.append(d)  
    print "Found " + str(len(devices)) + " devices."
    return devices

有任何想法吗?

您的一把钥匙太多了。 这应该足够了:

for deviceListJSON in getDeviceListFromserver()['mobile_device_group']['mobile_devices']:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM