This question already has answers here:
TypeError: string indices must be integers, not str // working with dict
(3个答案)
2年前关闭。
我正在尝试解析JSON变量
错误:-
(3个答案)
2年前关闭。
我正在尝试解析JSON变量
json_text
并尝试创建change_requests列表,但遇到以下错误?有人可以提供有关解决此问题的指南吗?import sys,os,json
from collections import OrderedDict
def main ():
change_request_list=[]
json_text=[OrderedDict([(u'branch', u'wlan-service.lnx.1.1-dev'), (u'change_request_list', [OrderedDict([(u'change_request', u'2028693'), (u'change_request_approval_status', OrderedDict([(u'comment', None), (u'status', None)]))])]), (u'component', u'platform_vendor_qcom-proprietary_ship_wlan_common-tools_wlan-service.lnx.1.1-dev'), (u'description', u'cnss_diag: Capture PKTLOG by default to sdcard'), (u'gerrit', u'1892910'), (u'gerrit_approval_status', OrderedDict([(u'comment', None), (u'status', None)])), (u'owner', u'c_rrajiv'), (u'project', u'platform/vendor/qcom-proprietary/ship/wlan/common-tools'), (u'url', u'https://review-android.quicinc.com/#/c/1892910')]), OrderedDict([(u'branch', u'wlan-service.lnx.1.1-dev'), (u'change_request_list', [OrderedDict([(u'change_request', u'2029177'), (u'change_request_approval_status', OrderedDict([(u'comment', None), (u'status', None)]))])]), (u'component', u'platform_vendor_qcom-proprietary_ship_wlan_common-tools_wlan-service.lnx.1.1-dev'), (u'description', u'pktlog: Close file decription before archiving and creating a new one'), (u'gerrit', u'1895098'), (u'gerrit_approval_status', OrderedDict([(u'comment', None), (u'status', None)])), (u'owner', u'c_rrajiv'), (u'project', u'platform/vendor/qcom-proprietary/ship/wlan/common-tools'), (u'url', u'https://review-android.quicinc.com/#/c/1895098')]), OrderedDict([(u'branch', u'wlan-service.lnx.1.1-dev'), (u'change_request_list', [OrderedDict([(u'change_request', u'2030712'), (u'change_request_approval_status', OrderedDict([(u'comment', None), (u'status', None)]))])]), (u'component', u'platform_vendor_qcom-proprietary_ship_wlan_common-tools_wlan-service.lnx.1.1-dev'), (u'description', u'cnss_diag: Fix copyright header'), (u'gerrit', u'1897531'), (u'gerrit_approval_status', OrderedDict([(u'comment', None), (u'status', None)])), (u'owner', u'c_rrajiv'), (u'project', u'platform/vendor/qcom-proprietary/ship/wlan/common-tools'), (u'url', u'https://review-android.quicinc.com/#/c/1897531')]), OrderedDict([(u'branch', u'wlan-service.lnx.1.1-dev'), (u'change_request_list', [OrderedDict([(u'change_request', u'2032453'), (u'change_request_approval_status', OrderedDict([(u'comment', None), (u'status', None)]))])]), (u'component', u'platform_vendor_qcom-proprietary_ship_wlan_common-tools_wlan-service.lnx.1.1-dev'), (u'description', u'cnss_diag: Printing newly backup packetlog file name.'), (u'gerrit', u'1905895'), (u'gerrit_approval_status', OrderedDict([(u'comment', None), (u'status', None)])), (u'owner', u'c_rrajiv'), (u'project', u'platform/vendor/qcom-proprietary/ship/wlan/common-tools'), (u'url', u'https://review-android.quicinc.com/#/c/1905895')]), OrderedDict([(u'branch', u'wlan-service.lnx.1.1-dev'), (u'change_request_list', [OrderedDict([(u'change_request', u'2033296'), (u'change_request_approval_status', OrderedDict([(u'comment', None), (u'status', None)]))])]), (u'component', u'platform_vendor_qcom-proprietary_ship_wlan_common-tools_wlan-service.lnx.1.1-dev'), (u'description', u'cnss_diag: Configuration for disabling pkt logging on sdcard'), (u'gerrit', u'1905931'), (u'gerrit_approval_status', OrderedDict([(u'comment', None), (u'status', None)])), (u'owner', u'c_rrajiv'), (u'project', u'platform/vendor/qcom-proprietary/ship/wlan/common-tools'), (u'url', u'https://review-android.quicinc.com/#/c/1905931')])]
for cr in json_text:
print cr
change_request = cr['change_request_list']['change_request']
change_request_list.append(change_request)
print change_request_list
if __name__ == '__main__':
main()
错误:-
Traceback (most recent call last):
File "json_parsing.py", line 12, in <module>
main()
File "json_parsing.py", line 8, in main
change_request = cr['change_request_list']['change_request']
TypeError: list indices must be integers, not str
最佳答案
您在json键之间缺少[0]
for cr in json_text:
print cr
change_request = cr['change_request_list'][0]['change_request']
change_request_list.append(change_request)
print change_request_list
09-12 14:26