Python 2.7 Ошибка при парсинге JSON

Тема в разделе "Perl, Python, Ruby", создана пользователем QuZ, 28 май 2018.

  1. QuZ

    QuZ Постоялец

    Регистр.:
    18 июл 2009
    Сообщения:
    87
    Симпатии:
    49
    Добрый день всем. Вылезает ошибка при парсинге JSON
    Платформа - python 2.7

    Код:
    import json
    
    json_data = '{"from": "backend", "timestamp": 1527440354242, "resource_instance_id": "7b1765a80d4d42758795f3a8541ef226", "message": ["action", {"add": "before", "last": "true"}, [{"status": "DELIVERY_ACK", "message": {"conversation": "\u0410\u0432"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "B251AF24F2628D2C1D7395426A750530"}, "messageTimestamp": "1527265381"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u0421!\u0441"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "014BC8B429A115D21CFBA78EFEA79D40"}, "messageTimestamp": "1527265807"}, {"status": "ERROR", "message": {"conversation": "33"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "74812A9BB79390F072E3E7A224D79AFC"}, "messageTimestamp": "1527265969"}, {"status": "ERROR", "message": {"conversation": "Eeheyd"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "36201035E0A6F928CA4F049B4127941F"}, "messageTimestamp": "1527265995"}, {"status": "ERROR", "message": {"conversation": "Echo \"Hellow wold\";"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "E89237FB1BD4FFFF878B362D15D98973"}, "messageTimestamp": "1527267040"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u0421\u043c\u0441"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "25B3B0AC36E1A201BCAEE475679EB14A"}, "messageTimestamp": "1527267065"}, {"status": "ERROR", "message": {"conversation": "Fh"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "C57EADF718894692E7019B996D9CDC7F"}, "messageTimestamp": "1527267236"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u0418\u043c\u0430\u043c\u0430"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "59B9DA99AEB8E7FE60438552A26D50DD"}, "messageTimestamp": "1527267276"}, {"status": "DELIVERY_ACK", "message": {"imageMessage": {"mimetype": "image/jpeg", "fileSha256": "j5X/FCbCdtNNlOMmonCrp9K3ZVr0MUYcqVzyUWYrL2E=", "directPath": "/v/t62.7118-24/30780953_2197934576898365_5242323143450896651_n.enc?oh=c2d965281eb5d777e5bdbf88dc79e433&oe=D08C8EAF", "url": "https://mmg-fna.whatsapp.net/d/f/Al5TSdxrIffDfyCYOydqZ4tLUNye851BhL-TsWdp3_uK.enc", "mediaKey": "uJP3cQKe3TfqHVW8bphIm6UszzA/bjgQbvGfCSdxzzs=", "firstScanSidecar": "342a9e4r8A2h+Q==", "firstScanLength": 7484, "height": 720, "width": 1280, "fileEncSha256": "agp2AGLz0CpYF2fs/x1TgGIAn54RyKTQlj8B/vQPDSE=", "fileLength": "80817", "jpegThumbnail": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAA4AGQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAA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cyyNM5y+FHJPU0o0O5+UgQAr/eJOffp/9apIdCkAIkniXOekQb+dO4/ZlcS2uVa2LmYdATirVvbSNFvdXjmznkE85/8ArU+PQkT79454x8qbePTrV63tFgP7uWZl6BXYEfyo5g9mVnhIaNWXeoUHJBGDz25zUKwIwcTIyNwAF3Nv/HHb3rU6ng4FTW9tLOsxiGfKTewLAHGQOATk9RwMnGT0BouLlMN7aCI7ViYjHZc0VrYx1JzRRddg5Tbt9I07fHEy20YPAZoiwHHqASeldEo8JaJpvmust/Kd4CophAIx2yMdffofas0HTrKESalqMVtv5WNPncgE9QASo47/AOFcX4mv47u4h/sqWbyASuJlBy2c5A/4FUxfcto9Mh0S31BY7j5bdWiVjDHuOwnsSzE5GcdunSrNv4fsraRZBukHQiUKy9x6V49/wkWp7NgvrlUC7dqzOBjp0zis+4uZLmQySsXbOcn1/wAmqF8z2m9bQwhiuZrCIo33d6oeOMnvWe+p+GrFmjeSBiOSAhl4HXoD0ryF7iQEnLLng4OB+QqB5i+NxzjpmjlDmSPRNW1DSLvUimkTEuATJGIyoQg44z/Lt9Kq49Bg+3euLt7sRuWM5hkGNp25HHFdHa3MksYbzEYHglTkUmrFqSZoIBuI2kU9FTsMe9VfMfGN3BpenByPYVNguWnijBIDCmOqryPrkGoTjAOOKeGGO1LUNBDhyWJkyTzz/jRU0bqF5Ut9DRSuwVjH8WpIl1EZN3mBdjgjGGBOR+v9Kypi1vaWcwJ5dmwfXI/woorSOyM3uZjuVJU9QcVNuYRgLiiihstRRC7guAXBAPOTUTyxI4KsDjn1oop3II5Z1knBBOAAORT1u2jH7ksrZ6g449KKKV2Tcuxa1dIygkOoHIYVuWuoC5wygHjkbhkUUVjVk4xui4O5ZF2vyqzgdcZqfccA44PpRRSpzclqUXrGeFISJcbt31oooqxH/9k="}}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "EE17A887C4339B71788EA650B11CC57E"}, "messageTimestamp": "1527267297"}, {"status": "ERROR", "message": {"conversation": "Fg"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "BDDA8756020184D74BA4876B3A65EBFE"}, "messageTimestamp": "1527270994"}, {"status": "ERROR", "message": {"conversation": "Frgr"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "3D63D861C708626F5BF6ACB916BA1209"}, "messageTimestamp": "1527315103"}, {"status": "ERROR", "message": {"conversation": "fh"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "6C070D18BD6ABE266B319FB8ECAC6569"}, "messageTimestamp": "1527315252"}, {"status": "ERROR", "message": {"conversation": "Ttttttffg"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "784B391DB7CF32875EF9F1A40A010F83"}, "messageTimestamp": "1527315272"}, {"status": "ERROR", "message": {"conversation": "Tg"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "BABFDF9A4DD88C3FE9E55A7F254D150E"}, "messageTimestamp": "1527315620"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u0421\u0430\u043c"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "1866627D548B87383B3BD552F9F27378"}, "messageTimestamp": "1527330634"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u041f\u0440\u0438\u0432\u0435\u0442"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "22D62218AA4DD2C7652D28F12D45B0E3"}, "messageTimestamp": "1527330784"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u041f\u0440\u0438\u0432\u0435\u0442"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "FE7C6991644E424ABD82935B390B09C1"}, "messageTimestamp": "1527331532"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u041c\u043c\u043c"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "92C1ABCEA4F0873603F897E8CCC1A7B3"}, "messageTimestamp": "1527332628"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u0410\u0430\u0430\u0430\u0430"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "97EC16D3281D6CBE7619CB0E04259A2B"}, "messageTimestamp": "1527412956"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u041f\u0440\u0438\u0432\u0435\u0442"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "6DFDF12D6A32D596112746A78896327D"}, "messageTimestamp": "1527413216"}]], "type": "message_received", "message_type": "binary"}';
    
    parsed_json = json.loads(json_data);
    print (parsed_json["from"]);
    
    При выполнении выдает ошибку:
     
    Последнее редактирование: 28 май 2018
  2. Maybe

    Maybe

    Moderator
    Регистр.:
    7 июл 2008
    Сообщения:
    1.170
    Симпатии:
    1.201
    Ошибка вроде здесь: {"conversation": "Echo \"Hellow wold\";"}

    Попробуйте в json loads засунуть еще dumps, как-то так: json.loads(json.dumps(data))
     
  3. QuZ

    QuZ Постоялец

    Регистр.:
    18 июл 2009
    Сообщения:
    87
    Симпатии:
    49
    Спасибо, уже пробовал.

    Traceback (most recent call last:(
    File "t6.py", line 10, in <module>
    print (parsed_json["from"]);
    TypeError: string indices must be integers


    Код:
    import json
    
    json_data = '{"from": "backend", "timestamp": 1527440354242, "resource_instance_id": "7b1765a80d4d42758795f3a8541ef226", "message": ["action", {"add": "before", "last": "true"}, [{"status": "DELIVERY_ACK", "message": {"conversation": "\u0410\u0432"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "B251AF24F2628D2C1D7395426A750530"}, "messageTimestamp": "1527265381"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u0421!\u0441"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "014BC8B429A115D21CFBA78EFEA79D40"}, "messageTimestamp": "1527265807"}, {"status": "ERROR", "message": {"conversation": "33"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "74812A9BB79390F072E3E7A224D79AFC"}, "messageTimestamp": "1527265969"}, {"status": "ERROR", "message": {"conversation": "Eeheyd"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "36201035E0A6F928CA4F049B4127941F"}, "messageTimestamp": "1527265995"}, {"status": "ERROR", "message": {"conversation": "Echo \"Hellow wold\";"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "E89237FB1BD4FFFF878B362D15D98973"}, "messageTimestamp": "1527267040"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u0421\u043c\u0441"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "25B3B0AC36E1A201BCAEE475679EB14A"}, "messageTimestamp": "1527267065"}, {"status": "ERROR", "message": {"conversation": "Fh"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "C57EADF718894692E7019B996D9CDC7F"}, "messageTimestamp": "1527267236"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u0418\u043c\u0430\u043c\u0430"}, "key": {"remoteJid": "[email protected]atsabe.net", "fromMe": true, "id": "59B9DA99AEB8E7FE60438552A26D50DD"}, "messageTimestamp": "1527267276"}, {"status": "DELIVERY_ACK", "message": {"imageMessage": {"mimetype": "image/jpeg", "fileSha256": "j5X/FCbCdtNNlOMmonCrp9K3ZVr0MUYcqVzyUWYrL2E=", "directPath": "/v/t62.7118-24/30780953_2197934576898365_5242323143450896651_n.enc?oh=c2d965281eb5d777e5bdbf88dc79e433&oe=D08C8EAF", "url": "https://mmg-fna.whatsapp.net/d/f/Al5TSdxrIffDfyCYOydqZ4tLUNye851BhL-TsWdp3_uK.enc", "mediaKey": "uJP3cQKe3TfqHVW8bphIm6UszzA/bjgQbvGfCSdxzzs=", "firstScanSidecar": "342a9e4r8A2h+Q==", "firstScanLength": 7484, "height": 720, "width": 1280, "fileEncSha256": "agp2AGLz0CpYF2fs/x1TgGIAn54RyKTQlj8B/vQPDSE=", "fileLength": "80817", "jpegThumbnail": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCg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ryPrkGoTjAOOKeGGO1LUNBDhyWJkyTzz/jRU0bqF5Ut9DRSuwVjH8WpIl1EZN3mBdjgjGGBOR+v9Kypi1vaWcwJ5dmwfXI/woorSOyM3uZjuVJU9QcVNuYRgLiiihstRRC7guAXBAPOTUTyxI4KsDjn1oop3II5Z1knBBOAAORT1u2jH7ksrZ6g449KKKV2Tcuxa1dIygkOoHIYVuWuoC5wygHjkbhkUUVjVk4xui4O5ZF2vyqzgdcZqfccA44PpRRSpzclqUXrGeFISJcbt31oooqxH/9k="}}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "EE17A887C4339B71788EA650B11CC57E"}, "messageTimestamp": "1527267297"}, {"status": "ERROR", "message": {"conversation": "Fg"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "BDDA8756020184D74BA4876B3A65EBFE"}, "messageTimestamp": "1527270994"}, {"status": "ERROR", "message": {"conversation": "Frgr"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "3D63D861C708626F5BF6ACB916BA1209"}, "messageTimestamp": "1527315103"}, {"status": "ERROR", "message": {"conversation": "fh"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "6C070D18BD6ABE266B319FB8ECAC6569"}, "messageTimestamp": "1527315252"}, {"status": "ERROR", "message": {"conversation": "Ttttttffg"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "784B391DB7CF32875EF9F1A40A010F83"}, "messageTimestamp": "1527315272"}, {"status": "ERROR", "message": {"conversation": "Tg"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "BABFDF9A4DD88C3FE9E55A7F254D150E"}, "messageTimestamp": "1527315620"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u0421\u0430\u043c"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "1866627D548B87383B3BD552F9F27378"}, "messageTimestamp": "1527330634"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u041f\u0440\u0438\u0432\u0435\u0442"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "22D62218AA4DD2C7652D28F12D45B0E3"}, "messageTimestamp": "1527330784"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u041f\u0440\u0438\u0432\u0435\u0442"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "FE7C6991644E424ABD82935B390B09C1"}, "messageTimestamp": "1527331532"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u041c\u043c\u043c"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "92C1ABCEA4F0873603F897E8CCC1A7B3"}, "messageTimestamp": "1527332628"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u0410\u0430\u0430\u0430\u0430"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "97EC16D3281D6CBE7619CB0E04259A2B"}, "messageTimestamp": "1527412956"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u041f\u0440\u0438\u0432\u0435\u0442"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "6DFDF12D6A32D596112746A78896327D"}, "messageTimestamp": "1527413216"}]], "type": "message_received", "message_type": "binary"}';
    
    #parsed_json = json.loads(json_data);
    parsed_json = json.loads(json.dumps(json_data));
         
    
    
    print (parsed_json["from"]);
     
  4. Maybe

    Maybe

    Moderator
    Регистр.:
    7 июл 2008
    Сообщения:
    1.170
    Симпатии:
    1.201
    Надо дальше ошибку ращбирать, смотрите данные, вы пытаетесь к индексу обратиться, а там текстовое значение.
    https://stackoverflow.com/questions...ing-typeerror-string-indices-must-be-integers
     
  5. RORC

    RORC

    Регистр.:
    14 июн 2013
    Сообщения:
    262
    Симпатии:
    84
    Ошибка связана с тем, что не правильно преобразовали данные. Отрезать часть строки можно только по числовому индексу.

    1) Приведите код к стандарту, это не php, почему тут точка с запятой после каждой строки?

    2) dumps преобразует данные и типы питона к формату json, тут не валидный json закодировали в строку повторно
    json.dumps(json_data) https://docs.python.org/2/library/json.html#encoders-and-decoders

    3) json_data = '{"from": "backend", ... - при загрузке через loads выдает ошибку, т.к. допущена ошибка в синтаксисе json.
     
  6. RORC

    RORC

    Регистр.:
    14 июн 2013
    Сообщения:
    262
    Симпатии:
    84
    Вот рабочий код, нужно использовать тип raw, объяснение
    Код:
    import json
    json_data = r'{"from": "backend", "timestamp": 1527440354242, "resource_instance_id": "7b1765a80d4d42758795f3a8541ef226", "message": ["action", {"add": "before", "last": "true"}, [{"status": "DELIVERY_ACK", "message": {"conversation": "\u0410\u0432"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "B251AF24F2628D2C1D7395426A750530"}, "messageTimestamp": "1527265381"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u0421!\u0441"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "014BC8B429A115D21CFBA78EFEA79D40"}, "messageTimestamp": "1527265807"}, {"status": "ERROR", "message": {"conversation": "33"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "74812A9BB79390F072E3E7A224D79AFC"}, "messageTimestamp": "1527265969"}, {"status": "ERROR", "message": {"conversation": "Eeheyd"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "36201035E0A6F928CA4F049B4127941F"}, "messageTimestamp": "1527265995"}, {"status": "ERROR", "message": {"conversation": "Echo \"Hellow wold\";"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "E89237FB1BD4FFFF878B362D15D98973"}, "messageTimestamp": "1527267040"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u0421\u043c\u0441"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "25B3B0AC36E1A201BCAEE475679EB14A"}, "messageTimestamp": "1527267065"}, {"status": "ERROR", "message": {"conversation": "Fh"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "C57EADF718894692E7019B996D9CDC7F"}, "messageTimestamp": "1527267236"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u0418\u043c\u0430\u043c\u0430"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "59B9DA99AEB8E7FE60438552A26D50DD"}, "messageTimestamp": "1527267276"}, {"status": "DELIVERY_ACK", "message": {"imageMessage": {"mimetype": "image/jpeg", "fileSha256": "j5X/FCbCdtNNlOMmonCrp9K3ZVr0MUYcqVzyUWYrL2E=", "directPath": "/v/t62.7118-24/30780953_2197934576898365_5242323143450896651_n.enc?oh=c2d965281eb5d777e5bdbf88dc79e433&oe=D08C8EAF", "url": "https://mmg-fna.whatsapp.net/d/f/Al5TSdxrIffDfyCYOydqZ4tLUNye851BhL-TsWdp3_uK.enc", "mediaKey": "uJP3cQKe3TfqHVW8bphIm6UszzA/bjgQbvGfCSdxzzs=", "firstScanSidecar": "342a9e4r8A2h+Q==", "firstScanLength": 7484, "height": 720, "width": 1280, "fileEncSha256": "agp2AGLz0CpYF2fs/x1TgGIAn54RyKTQlj8B/vQPDSE=", "fileLength": "80817", "jpegThumbnail": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAA4AGQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoE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dATirVvbSNFvdXjmznkE85/8ArU+PQkT79454x8qbePTrV63tFgP7uWZl6BXYEfyo5g9mVnhIaNWXeoUHJBGDz25zUKwIwcTIyNwAF3Nv/HHb3rU6ng4FTW9tLOsxiGfKTewLAHGQOATk9RwMnGT0BouLlMN7aCI7ViYjHZc0VrYx1JzRRddg5Tbt9I07fHEy20YPAZoiwHHqASeldEo8JaJpvmust/Kd4CophAIx2yMdffofas0HTrKESalqMVtv5WNPncgE9QASo47/AOFcX4mv47u4h/sqWbyASuJlBy2c5A/4FUxfcto9Mh0S31BY7j5bdWiVjDHuOwnsSzE5GcdunSrNv4fsraRZBukHQiUKy9x6V49/wkWp7NgvrlUC7dqzOBjp0zis+4uZLmQySsXbOcn1/wAmqF8z2m9bQwhiuZrCIo33d6oeOMnvWe+p+GrFmjeSBiOSAhl4HXoD0ryF7iQEnLLng4OB+QqB5i+NxzjpmjlDmSPRNW1DSLvUimkTEuATJGIyoQg44z/Lt9Kq49Bg+3euLt7sRuWM5hkGNp25HHFdHa3MksYbzEYHglTkUmrFqSZoIBuI2kU9FTsMe9VfMfGN3BpenByPYVNguWnijBIDCmOqryPrkGoTjAOOKeGGO1LUNBDhyWJkyTzz/jRU0bqF5Ut9DRSuwVjH8WpIl1EZN3mBdjgjGGBOR+v9Kypi1vaWcwJ5dmwfXI/woorSOyM3uZjuVJU9QcVNuYRgLiiihstRRC7guAXBAPOTUTyxI4KsDjn1oop3II5Z1knBBOAAORT1u2jH7ksrZ6g449KKKV2Tcuxa1dIygkOoHIYVuWuoC5wygHjkbhkUUVjVk4xui4O5ZF2vyqzgdcZqfccA44PpRRSpzclqUXrGeFISJcbt31oooqxH/9k="}}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "EE17A887C4339B71788EA650B11CC57E"}, "messageTimestamp": "1527267297"}, {"status": "ERROR", "message": {"conversation": "Fg"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "BDDA8756020184D74BA4876B3A65EBFE"}, "messageTimestamp": "1527270994"}, {"status": "ERROR", "message": {"conversation": "Frgr"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "3D63D861C708626F5BF6ACB916BA1209"}, "messageTimestamp": "1527315103"}, {"status": "ERROR", "message": {"conversation": "fh"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "6C070D18BD6ABE266B319FB8ECAC6569"}, "messageTimestamp": "1527315252"}, {"status": "ERROR", "message": {"conversation": "Ttttttffg"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "784B391DB7CF32875EF9F1A40A010F83"}, "messageTimestamp": "1527315272"}, {"status": "ERROR", "message": {"conversation": "Tg"}, "key": {"remoteJid": "[email protected]", "fromMe": false, "id": "BABFDF9A4DD88C3FE9E55A7F254D150E"}, "messageTimestamp": "1527315620"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u0421\u0430\u043c"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "1866627D548B87383B3BD552F9F27378"}, "messageTimestamp": "1527330634"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u041f\u0440\u0438\u0432\u0435\u0442"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "22D62218AA4DD2C7652D28F12D45B0E3"}, "messageTimestamp": "1527330784"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u041f\u0440\u0438\u0432\u0435\u0442"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "FE7C6991644E424ABD82935B390B09C1"}, "messageTimestamp": "1527331532"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u041c\u043c\u043c"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "92C1ABCEA4F0873603F897E8CCC1A7B3"}, "messageTimestamp": "1527332628"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u0410\u0430\u0430\u0430\u0430"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "97EC16D3281D6CBE7619CB0E04259A2B"}, "messageTimestamp": "1527412956"}, {"status": "DELIVERY_ACK", "message": {"conversation": "\u041f\u0440\u0438\u0432\u0435\u0442"}, "key": {"remoteJid": "[email protected]", "fromMe": true, "id": "6DFDF12D6A32D596112746A78896327D"}, "messageTimestamp": "1527413216"}]], "type": "message_received", "message_type": "binary"}'
    parsed_json = json.loads(json_data)
    # print parsed_json
    print (parsed_json["from"])
    
     
  7. QuZ

    QuZ Постоялец

    Регистр.:
    18 июл 2009
    Сообщения:
    87
    Симпатии:
    49
    1 - На php пишу, по этому, привычка. А питон не жаловался. Вот и скобки, точки с запятой и унаследовались у меня в коде.

    Сегодня посмотрю, отвечу всем.
     
  8. RORC

    RORC

    Регистр.:
    14 июн 2013
    Сообщения:
    262
    Симпатии:
    84
    он и не будет, но есть pep8 которого лучше придерживаться, т.к. править ошибки когда и так все подчеркнуто анализатором не очень удобно.

    И для тестов поставьте Jupyter Notebook, при разработке удобный инструмент.