import json
test=u'''{"message":"ok","nu":"773022557706794","ischeck":"0","condition":"00","com":"shentong","status":"200","state":"0","data":[{"time":"2020-01-12 23:37:44","ftime":"2020-01-12 23:37:44","context":"[沈阳市]沈阳市【沈阳转运中心】,正发往【凤城市】","location":"沈阳市"},{"time":"2020-01-12 23:25:15","ftime":"2020-01-12 23:25:15","context":"[沈阳市]到沈阳市【沈阳转运中心】","location":"沈阳市"},{"time":"2020-01-11 01:20:11","ftime":"2020-01-11 01:20:11","context":"[郑州市]郑州市【郑州转运中心】,正发往【沈阳转运中心】","location":"郑州市"},{"time":"2020-01-11 01:13:16","ftime":"2020-01-11 01:13:16","context":"[郑州市]郑州市【郑州转运中心】,正发往【沈阳转运中心】","location":"郑州市"},{"time":"2020-01-10 22:30:26","ftime":"2020-01-10 22:30:26","context":"[郑州市]郑州市【郑州转运中心】,正发往【沈阳转运中心】","location":"郑州市"},{"time":"2020-01-10 22:25:51","ftime":"2020-01-10 22:25:51","context":"[郑州市]到郑州市【郑州转运中心】","location":"郑州市"},{"time":"2020-01-09 16:51:27","ftime":"2020-01-09 16:51:27","context":"[洛阳市]洛阳市【洛阳集散中心】,正发往【郑州转运中心】","location":"洛阳市"},{"time":"2020-01-09 16:51:16","ftime":"2020-01-09 16:51:16","context":"[洛阳市]到洛阳市【洛阳集散中心】","location":"洛阳市"},{"time":"2020-01-09 12:13:43","ftime":"2020-01-09 12:13:43","context":"[济源市]济源市【玉泉区分部】,【张超超/18790042837】已揽收","location":"济源市"}]}'''
#把字符串转载为json:loads
json_1=json.loads(test)
#把json格式化为str:dumps
json_2=json.dumps(json_1,ensure_ascii=False,indent=4)
#json中的索引
print(json_1['data'])
print(json_2)
#json中的遍历
for itme in json_1['data']:
print(itme)
#把json转为字典,进行遍历
ls_all=[]
for dic in json_1['data']:
ls=[]
for k,v in dict(dic).items():
#这里的dic[k]和v是同一个变量
print(dic[k],end='')
ls.append(v)
print()
#把一个列表放入另一个列表
ls_all.append(ls)
print(ls_all)
输出内容
[{'time': '2020-01-12 23:37:44', 'ftime': '2020-01-12 23:37:44', 'context': '[沈阳市]沈阳市【沈阳转运中心】,正发往【凤城市】', 'location': '沈阳市'}, {'time': '2020-01-12 23:25:15', 'ftime': '2020-01-12 23:25:15', 'context': '[沈阳市]到沈阳市【沈阳转运中心】', 'location': '沈阳市'}, {'time': '2020-01-11 01:20:11', 'ftime': '2020-01-11 01:20:11', 'context': '[郑州市]郑州市【郑州转运中心】,正发往【沈阳转运中心】', 'location': '郑州市'}, {'time': '2020-01-11 01:13:16', 'ftime': '2020-01-11 01:13:16', 'context': '[郑州市]郑州市【郑州转运中心】,正发往【沈阳转运中心】', 'location': '郑州市'}, {'time': '2020-01-10 22:30:26', 'ftime': '2020-01-10 22:30:26', 'context': '[郑州市]郑州市【郑州转运中心】,正发往【沈阳转运中心】', 'location': '郑州市'}, {'time': '2020-01-10 22:25:51', 'ftime': '2020-01-10 22:25:51', 'context': '[郑州市]到郑州市【郑州转运中心】', 'location': '郑州市'}, {'time': '2020-01-09 16:51:27', 'ftime': '2020-01-09 16:51:27', 'context': '[洛阳市]洛阳市【洛阳集散中心】,正发往【郑州转运中心】', 'location': '洛阳市'}, {'time': '2020-01-09 16:51:16', 'ftime': '2020-01-09 16:51:16', 'context': '[洛阳市]到洛阳市【洛阳集散中心】', 'location': '洛阳市'}, {'time': '2020-01-09 12:13:43', 'ftime': '2020-01-09 12:13:43', 'context': '[济源市]济源市【玉泉区分部】,【张超超/18790042837】已揽收', 'location': '济源市'}]
{
"message": "ok",
"nu": "773022557706794",
"ischeck": "0",
"condition": "00",
"com": "shentong",
"status": "200",
"state": "0",
"data": [
{
"time": "2020-01-12 23:37:44",
"ftime": "2020-01-12 23:37:44",
"context": "[沈阳市]沈阳市【沈阳转运中心】,正发往【凤城市】",
"location": "沈阳市"
},
{
"time": "2020-01-12 23:25:15",
"ftime": "2020-01-12 23:25:15",
"context": "[沈阳市]到沈阳市【沈阳转运中心】",
"location": "沈阳市"
},
{
"time": "2020-01-11 01:20:11",
"ftime": "2020-01-11 01:20:11",
"context": "[郑州市]郑州市【郑州转运中心】,正发往【沈阳转运中心】",
"location": "郑州市"
},
{
"time": "2020-01-11 01:13:16",
"ftime": "2020-01-11 01:13:16",
"context": "[郑州市]郑州市【郑州转运中心】,正发往【沈阳转运中心】",
"location": "郑州市"
},
{
"time": "2020-01-10 22:30:26",
"ftime": "2020-01-10 22:30:26",
"context": "[郑州市]郑州市【郑州转运中心】,正发往【沈阳转运中心】",
"location": "郑州市"
},
{
"time": "2020-01-10 22:25:51",
"ftime": "2020-01-10 22:25:51",
"context": "[郑州市]到郑州市【郑州转运中心】",
"location": "郑州市"
},
{
"time": "2020-01-09 16:51:27",
"ftime": "2020-01-09 16:51:27",
"context": "[洛阳市]洛阳市【洛阳集散中心】,正发往【郑州转运中心】",
"location": "洛阳市"
},
{
"time": "2020-01-09 16:51:16",
"ftime": "2020-01-09 16:51:16",
"context": "[洛阳市]到洛阳市【洛阳集散中心】",
"location": "洛阳市"
},
{
"time": "2020-01-09 12:13:43",
"ftime": "2020-01-09 12:13:43",
"context": "[济源市]济源市【玉泉区分部】,【张超超/18790042837】已揽收",
"location": "济源市"
}
]
}
{'time': '2020-01-12 23:37:44', 'ftime': '2020-01-12 23:37:44', 'context': '[沈阳市]沈阳市【沈阳转运中心】,正发往【凤城市】', 'location': '沈阳市'}
{'time': '2020-01-12 23:25:15', 'ftime': '2020-01-12 23:25:15', 'context': '[沈阳市]到沈阳市【沈阳转运中心】', 'location': '沈阳市'}
{'time': '2020-01-11 01:20:11', 'ftime': '2020-01-11 01:20:11', 'context': '[郑州市]郑州市【郑州转运中心】,正发往【沈阳转运中心】', 'location': '郑州市'}
{'time': '2020-01-11 01:13:16', 'ftime': '2020-01-11 01:13:16', 'context': '[郑州市]郑州市【郑州转运中心】,正发往【沈阳转运中心】', 'location': '郑州市'}
{'time': '2020-01-10 22:30:26', 'ftime': '2020-01-10 22:30:26', 'context': '[郑州市]郑州市【郑州转运中心】,正发往【沈阳转运中心】', 'location': '郑州市'}
{'time': '2020-01-10 22:25:51', 'ftime': '2020-01-10 22:25:51', 'context': '[郑州市]到郑州市【郑州转运中心】', 'location': '郑州市'}
{'time': '2020-01-09 16:51:27', 'ftime': '2020-01-09 16:51:27', 'context': '[洛阳市]洛阳市【洛阳集散中心】,正发往【郑州转运中心】', 'location': '洛阳市'}
{'time': '2020-01-09 16:51:16', 'ftime': '2020-01-09 16:51:16', 'context': '[洛阳市]到洛阳市【洛阳集散中心】', 'location': '洛阳市'}
{'time': '2020-01-09 12:13:43', 'ftime': '2020-01-09 12:13:43', 'context': '[济源市]济源市【玉泉区分部】,【张超超/18790042837】已揽收', 'location': '济源市'}
2020-01-12 23:37:442020-01-12 23:37:44[沈阳市]沈阳市【沈阳转运中心】,正发往【凤城市】沈阳市
2020-01-12 23:25:152020-01-12 23:25:15[沈阳市]到沈阳市【沈阳转运中心】沈阳市
2020-01-11 01:20:112020-01-11 01:20:11[郑州市]郑州市【郑州转运中心】,正发往【沈阳转运中心】郑州市
2020-01-11 01:13:162020-01-11 01:13:16[郑州市]郑州市【郑州转运中心】,正发往【沈阳转运中心】郑州市
2020-01-10 22:30:262020-01-10 22:30:26[郑州市]郑州市【郑州转运中心】,正发往【沈阳转运中心】郑州市
2020-01-10 22:25:512020-01-10 22:25:51[郑州市]到郑州市【郑州转运中心】郑州市
2020-01-09 16:51:272020-01-09 16:51:27[洛阳市]洛阳市【洛阳集散中心】,正发往【郑州转运中心】洛阳市
2020-01-09 16:51:162020-01-09 16:51:16[洛阳市]到洛阳市【洛阳集散中心】洛阳市
2020-01-09 12:13:432020-01-09 12:13:43[济源市]济源市【玉泉区分部】,【张超超/18790042837】已揽收济源市
[['2020-01-12 23:37:44', '2020-01-12 23:37:44', '[沈阳市]沈阳市【沈阳转运中心】,正发往【凤城市】', '沈阳市'], ['2020-01-12 23:25:15', '2020-01-12 23:25:15', '[沈阳市]到沈阳市【沈阳转运中心】', '沈阳市'], ['2020-01-11 01:20:11', '2020-01-11 01:20:11', '[郑州市]郑州市【郑州转运中心】,正发往【沈阳转运中心】', '郑州市'], ['2020-01-11 01:13:16', '2020-01-11 01:13:16', '[郑州市]郑州市【郑州转运中心】,正发往【沈阳转运中心】', '郑州市'], ['2020-01-10 22:30:26', '2020-01-10 22:30:26', '[郑州市]郑州市【郑州转运中心】,正发往【沈阳转运中心】', '郑州市'], ['2020-01-10 22:25:51', '2020-01-10 22:25:51', '[郑州市]到郑州市【郑州转运中心】', '郑州市'], ['2020-01-09 16:51:27', '2020-01-09 16:51:27', '[洛阳市]洛阳市【洛阳集散中心】,正发往【郑州转运中心】', '洛阳市'], ['2020-01-09 16:51:16', '2020-01-09 16:51:16', '[洛阳市]到洛阳市【洛阳集散中心】', '洛阳市'], ['2020-01-09 12:13:43', '2020-01-09 12:13:43', '[济源市]济源市【玉泉区分部】,【张超超/18790042837】已揽收', '济源市']]
Process finished with exit code 0
网友评论