import json # Hilfsfunktion, Liste und Suchtitel als Argumente def title_to_id(search_list, title): for item in search_list: if item["title"] == title: return item["id"] # Beispiel JSON, als String: json_str=""" { "items": [{ "id": "Y2lzY29zcGFyazovL3VzL1JPT00vODllYzJlODItNGU5Ni0zYWJkLTgzMTktNGE4YjNiY2VlOWIx", "title": "Oliver Olmesdahl (oolmesda)", "type": "direct", "isLocked": false, "lastActivity": "2016-04-12T11:10:44.063Z", "created": "2016-04-12T11:10:13.713Z" }, { "id": "Y2lzY29zcGFyazovL3VzL1JPT00vMzExMGViYWUtY2NjNi0zM2ViLWIyZjYtMmY0ZDA5YTIyODBl", "title": "Reto Kolb (rkolb)", "type": "direct", "isLocked": false, "lastActivity": "2016-04-11T16:19:06.413Z", "created": "2016-04-11T16:18:58.122Z" }] } """ # JSON in Dictionary laden: items_dict = json.loads(json_str) # Hinter Key "items" steckt eine Liste: items_list = items_dict["items"] # Tests: id = title_to_id(items_list, "titelgibtsgarnicht") print id # Should be None id = title_to_id(items_list, "Oliver Olmesdahl (oolmesda)") print id # Should be correct id
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question