{ "list":{ "messageHeader":{ "intKey":"ff02e053-e1c9-434f-9ed6-f5e240d566b9", "sendName":"1141", "receiveName":"1129", "sendDate":"20230530", "sendTime":"165146" }, "items":[ { "costCenterCode":"ZGAE", "receiptsNumber":"CK20000446230500164", "amount":35.4, "materialDesc":"压下辊装配", "projectNumber":" ", "logicArea":"42", "receivingDepartment":"设备室", "materialCode":"C0213743", "inventory":"SGGGZXK001", "expenseItemCode":"BJXL", "materialMidTypeDesc":"辊子", "matQuantity":1, "receiptsType":"NMLC", "materialBigType":"C06", "materialBigTypeDesc":"辊子", "costAccount":"610001", "lineNumber":"1", "materialMidType":"C0601" }, { "costCenterCode":"ZGAE", "receiptsNumber":"CK20000446230500164", "amount":35.4, "materialDesc":"压下辊装配", "projectNumber":" ", "logicArea":"42", "receivingDepartment":"设备室", "materialCode":"C0213743", "inventory":"SGGGZXK001", "expenseItemCode":"BJXL", "materialMidTypeDesc":"辊子", "matQuantity":1, "receiptsType":"NMLC", "materialBigType":"C06", "materialBigTypeDesc":"辊子", "costAccount":"610001", "lineNumber":"1", "materialMidType":"C0601" } ] } }如何用json解析遍历
时间: 2023-08-31 18:12:19 浏览: 49
PB18051113-林成渊-第二次作业1
可以使用以下代码来解析和遍历上面的JSON数据:
```python
import json
# JSON数据
data = '''
{
"list": {
"messageHeader": {
"intKey": "ff02e053-e1c9-434f-9ed6-f5e240d566b9",
"sendName": "1141",
"receiveName": "1129",
"sendDate": "20230530",
"sendTime": "165146"
},
"items": [
{
"costCenterCode": "ZGAE",
"receiptsNumber": "CK20000446230500164",
"amount": 35.4,
"materialDesc": "压下辊装配",
"projectNumber": " ",
"logicArea": "42",
"receivingDepartment": "设备室",
"materialCode": "C0213743",
"inventory": "SGGGZXK001",
"expenseItemCode": "BJXL",
"materialMidTypeDesc": "辊子",
"matQuantity": 1,
"receiptsType": "NMLC",
"materialBigType": "C06",
"materialBigTypeDesc": "辊子",
"costAccount": "610001",
"lineNumber": "1",
"materialMidType": "C0601"
},
{
"costCenterCode": "ZGAE",
"receiptsNumber": "CK20000446230500164",
"amount": 35.4,
"materialDesc": "压下辊装配",
"projectNumber": " ",
"logicArea": "42",
"receivingDepartment": "设备室",
"materialCode": "C0213743",
"inventory": "SGGGZXK001",
"expenseItemCode": "BJXL",
"materialMidTypeDesc": "辊子",
"matQuantity": 1,
"receiptsType": "NMLC",
"materialBigType": "C06",
"materialBigTypeDesc": "辊子",
"costAccount": "610001",
"lineNumber": "1",
"materialMidType": "C0601"
}
]
}
}
'''
# 解析JSON数据
json_data = json.loads(data)
# 遍历items数组
for item in json_data['list']['items']:
print('costCenterCode:', item['costCenterCode'])
print('receiptsNumber:', item['receiptsNumber'])
print('amount:', item['amount'])
print('materialDesc:', item['materialDesc'])
print('projectNumber:', item['projectNumber'])
print('logicArea:', item['logicArea'])
print('receivingDepartment:', item['receivingDepartment'])
print('materialCode:', item['materialCode'])
print('inventory:', item['inventory'])
print('expenseItemCode:', item['expenseItemCode'])
print('materialMidTypeDesc:', item['materialMidTypeDesc'])
print('matQuantity:', item['matQuantity'])
print('receiptsType:', item['receiptsType'])
print('materialBigType:', item['materialBigType'])
print('materialBigTypeDesc:', item['materialBigTypeDesc'])
print('costAccount:', item['costAccount'])
print('lineNumber:', item['lineNumber'])
print('materialMidType:', item['materialMidType'])
print('\n')
```
输出:
```
costCenterCode: ZGAE
receiptsNumber: CK20000446230500164
amount: 35.4
materialDesc: 压下辊装配
projectNumber:
logicArea: 42
receivingDepartment: 设备室
materialCode: C0213743
inventory: SGGGZXK001
expenseItemCode: BJXL
materialMidTypeDesc: 辊子
matQuantity: 1
receiptsType: NMLC
materialBigType: C06
materialBigTypeDesc: 辊子
costAccount: 610001
lineNumber: 1
materialMidType: C0601
costCenterCode: ZGAE
receiptsNumber: CK20000446230500164
amount: 35.4
materialDesc: 压下辊装配
projectNumber:
logicArea: 42
receivingDepartment: 设备室
materialCode: C0213743
inventory: SGGGZXK001
expenseItemCode: BJXL
materialMidTypeDesc: 辊子
matQuantity: 1
receiptsType: NMLC
materialBigType: C06
materialBigTypeDesc: 辊子
costAccount: 610001
lineNumber: 1
materialMidType: C0601
```
阅读全文