我是新的json解析从python中的http api。当前我已经在python中将http内容解析为字符串,其中包含json对象数组我的代码如下所示
import json
from urllib.request import urlopen
apilink=urlopen("api link")
data=json.loads(apilink.read().decode())
print(data)
我当前的输出是
{'Message': 'Success', 'Data': '[{"Did":"c055c3d2f3314725b69965e6c55adb5b","InsertedDate":"2017-08-02 7:27:11 AM","UpdatedDate":"2017-08-02 9:33:16 AM","CreatedBy":"1","UpdatedBy":"1","Name":"Hello World","ModuleName":"Rpt_Hello_World","ApplicationName":"Asset Inventory","Published":"true","UserId":"1","PostProcessor":""}]', 'Status': 'Success'}
但我想只提取属性'数据'那是json数组
'Data': '[{"Did":"c055c3d2f3314725b69965e6c55adb5b","InsertedDate":"2017-08-02 7:27:11 AM","UpdatedDate":"2017-08-02 9:33:16 AM","CreatedBy":"1","UpdatedBy":"1","Name":"Hello World","ModuleName":"Rpt_Hello_World","ApplicationName":"Asset Inventory","Published":"true","UserId":"1","PostProcessor":""}]'
令人满意的部分是
[{"":" c055c3d2f3314725b69965e6c55adb5b"," InsertedDate":" 2017-08-02 7:27:11 AM" ," UpdatedDate":" 2017-08-02 9:33:16 AM"," CreatedBy":" 1",&#34 ; UpdatedBy":" 1","姓名":" Hello World"," ModuleName":" Rpt_Hello_World&#34 ;," ApplicationName":"资产库存","已发布":" true"," UserId":&# 34; 1"" PostProcessor中":""}]
请帮助我解决这个问题。 谢谢
答案 0 :(得分:2)
data
是一本字典。使用dict索引。您需要与Data
关联的值:
In [876]: data['Data']
Out[876]: '[{"Did":"c055c3d2f3314725b69965e6c55adb5b","InsertedDate":"2017-08-02 7:27:11 AM","UpdatedDate":"2017-08-02 9:33:16 AM","CreatedBy":"1","UpdatedBy":"1","Name":"Hello World","ModuleName":"Rpt_Hello_World","ApplicationName":"Asset Inventory","Published":"true","UserId":"1","PostProcessor":""}]'
这是一个字符串。您可以再次使用json.loads
。
In [877]: json.loads(data['Data'])
Out[877]:
[{'ApplicationName': 'Asset Inventory',
'CreatedBy': '1',
'Did': 'c055c3d2f3314725b69965e6c55adb5b',
'InsertedDate': '2017-08-02 7:27:11 AM',
'ModuleName': 'Rpt_Hello_World',
'Name': 'Hello World',
'PostProcessor': '',
'Published': 'true',
'UpdatedBy': '1',
'UpdatedDate': '2017-08-02 9:33:16 AM',
'UserId': '1'}]
答案 1 :(得分:0)
那很简单
format: jsonName[attributename]
data['Data']