お世話になっております。
LINE Messaging APIからJSONファイルを取得する所まではうまくいきましたが、この複雑な辞書からデータを取り出す所でつまづきました。
Python
#!/usr/local/bin/python3.7import json, os, sys, cgi import sqlite3 if os.environ['REQUEST_METHOD'] == 'POST': length, _ = cgi.parse_header(os.environ['CONTENT_LENGTH']) data = sys.stdin.buffer.read(int(length)) json_str = data.decode("utf-8") json_data = json.loads(json_str) destination = json_data["destination"] events = json_data["events"] ### ここでエラーになるようです### event_type = json_data["events"][0]["type"]###################### print ("Content-type: application/json\n") print ("{}")
Pythonの辞書
{'destination': '***', 'events': [ { 'type': 'message', 'message': { 'type': 'text', 'id': '***', 'text': 'ようこそ' }, 'webhookEventId': '***', 'deliveryContext': {'isRedelivery': False}, 'timestamp': 1656995173364, 'source': { 'type': 'user', 'userId': '***' }, 'replyToken': '***', 'mode': 'active' } ]}
変数event_typeにはmessageという文字列が入ると考えますが、エラーとなってしまいます。
Pythonの辞書の扱い方の話だと思いますが、どのサイトを参考にしても上記のような書き方であり、エラーが解決しません。
どのように修正すべきか、何卒ご教授下さいませ。
0 コメント