Python 文本解析从onenote回复字符串

2024-06-26 00:22:08 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将这样的字符串解析为数据结构。我应该使用什么样的分析工具?在

u'{\r\n  "@odata.context":"https://www.onenote.com/api/v1.0/$metadata#me/notes/notebooks(\'0-AB87696357344A7E%212879\')/sections(parentNotebook(id,name,self),parentSectionGroup(id,name,self))","value":[\r\n    {\r\n      "id":"0-AB87696357344A7E!2881","self":"https://www.onenote.com/api/v1.0/me/notes/sections/0-AB87696357344A7E!2881","createdTime":"2017-05-18T01:14:32.977Z","name":"Untitled Section","createdBy":"Jason","createdByIdentity":{\r\n        "user":{\r\n          "id":"AB87696357344A7E","displayName":"Jason"\r\n        }\r\n      },"lastModifiedBy":"Jason","lastModifiedByIdentity":{\r\n        "user":{\r\n          "id":"AB87696357344A7E","displayName":"Jason"\r\n        }\r\n      },"lastModifiedTime":"2017-05-18T02:19:00.587Z","isDefault":false,"pagesUrl":"https://www.onenote.com/api/v1.0/me/notes/sections/0-AB87696357344A7E!2881/pages","parentNotebook@odata.context":"https://www.onenote.com/api/v1.0/$metadata#me/notes/notebooks(\'0-AB87696357344A7E%212879\')/sections(\'0-AB87696357344A7E%212881\')/parentNotebook(id,name,self)/$entity","parentNotebook":{\r\n        "id":"0-AB87696357344A7E!2879","name":"Companies and sectors","self":"https://www.onenote.com/api/v1.0/me/notes/notebooks/0-AB87696357344A7E!2879"\r\n      },"parentSectionGroup@odata.context":"https://www.onenote.com/api/v1.0/$metadata#me/notes/notebooks(\'0-AB87696357344A7E%212879\')/sections(\'0-AB87696357344A7E%212881\')/parentSectionGroup(id,name,self)/$entity","parentSectionGroup":null\r\n    }\r\n  ]\r\n}'

Tags: namehttpsselfcomapiidwwwnotes
1条回答
网友
1楼 · 发布于 2024-06-26 00:22:08

正如SuperSaiyan在评论中提到的,这是一个JSON字符串。因此可以通过json库轻松解析:

import json
json_data = json.loads(
    u'{\r\n  "@odata.context":"https://www.onenote.com/api/v1.0/$metadata#me/notes/notebooks(\'0-AB87696357344A7E%212879\')/sections(parentNotebook(id,name,self),parentSectionGroup(id,name,self))","value":[\r\n    {\r\n      "id":"0-AB87696357344A7E!2881","self":"https://www.onenote.com/api/v1.0/me/notes/sections/0-AB87696357344A7E!2881","createdTime":"2017-05-18T01:14:32.977Z","name":"Untitled Section","createdBy":"Jason","createdByIdentity":{\r\n        "user":{\r\n          "id":"AB87696357344A7E","displayName":"Jason"\r\n        }\r\n      },"lastModifiedBy":"Jason","lastModifiedByIdentity":{\r\n        "user":{\r\n          "id":"AB87696357344A7E","displayName":"Jason"\r\n        }\r\n      },"lastModifiedTime":"2017-05-18T02:19:00.587Z","isDefault":false,"pagesUrl":"https://www.onenote.com/api/v1.0/me/notes/sections/0-AB87696357344A7E!2881/pages","parentNotebook@odata.context":"https://www.onenote.com/api/v1.0/$metadata#me/notes/notebooks(\'0-AB87696357344A7E%212879\')/sections(\'0-AB87696357344A7E%212881\')/parentNotebook(id,name,self)/$entity","parentNotebook":{\r\n        "id":"0-AB87696357344A7E!2879","name":"Companies and sectors","self":"https://www.onenote.com/api/v1.0/me/notes/notebooks/0-AB87696357344A7E!2879"\r\n      },"parentSectionGroup@odata.context":"https://www.onenote.com/api/v1.0/$metadata#me/notes/notebooks(\'0-AB87696357344A7E%212879\')/sections(\'0-AB87696357344A7E%212881\')/parentSectionGroup(id,name,self)/$entity","parentSectionGroup":null\r\n    }\r\n  ]\r\n}'
)
print(type(json_data))
print(json_data['value'][0]['createdBy'])

结果:

^{pr2}$

相关问题 更多 >