Python:如何将json unicode字典转换为python rdictionary

时间:2014-08-05 08:58:08

标签: python json unicode

我有这个json响应,由unicode字典组成,无法将其转换为python字典:

u'{
    "event": {
        "event_type": "signature_request_sent",
        "event_time": "1407227830",
        "event_hash": "567b6a342e29ceb8ad3a9641ccd27968f04f2c744c7dd3af8a1922f4eaa5d337",
        "event_metadata": {
            "related_signature_id": null,
            "reported_for_account_id": null,
            "reported_for_app_id": "290e6bb260fed5613b68066084fc77ff",
            "event_message": null
        }
    },
    "account_guid": null,
    "client_id": "290e6bb260fed5613b68066084fc77ff",
    "signature_request": {

        "title": "My First embedded signature request with a template",
        "original_title": "My First embedded signature request with a template",
        "subject": "My First embedded signature request with a template",
        "message": "Awesome, right?",
        "test_mode": true,
        "is_complete": false,
        "has_error": false,
        "custom_fields": [

        ],
        "response_data": [

        ],
        "signing_url": null,
        "signing_redirect_url": null,
        "final_copy_uri": "",
        "signatures": [
            {
                "signature_id": "9c5d829af9e0566134dd28bd064a2fe8",
                "has_pin": false,
                "signer_email_address": "napopasas@gmail.com",
                "order": null,
                "status_code": "awaiting_signature",
                "signed_at": null,
                "last_viewed_at": null,
                "last_reminded_at": null
            }
        ],
        "cc_email_addresses": [

        ]
    }
}'

尝试以下解决方案但未成功(字符串为ValueError)

Convert unicode string dictionary into dictionary in python

2 个答案:

答案 0 :(得分:0)

使用json.loads()

>>> a = """{
...     "event": {
...         "event_type": "signature_request_sent",
...         "event_time": "1407227830",
...         "event_hash": "567b6a342e29ceb8ad3a9641ccd27968f04f2c744c7dd3af8a1922f4eaa5d337",
...         "event_metadata": {
...             "related_signature_id": null,
...             "reported_for_account_id": null,
...             "reported_for_app_id": "290e6bb260fed5613b68066084fc77ff",
...             "event_message": null
...         }
        "original_title": "My First embedded signature request with a template",
...     },
...     "account_guid": null,
...     "client_id": "290e6bb260fed5613b68066084fc77ff",
...     "signature_request": {
...
...         "title": "My First embedded signature request with a template",
...         "original_title": "My First embedded signature request with a template",
...         "subject": "My First embedded signature request with a template",
...         "message": "Awesome, right?",
...         "test_mode": true,
...         "is_complete": false,
...         "has_error": false,
...         "custom_fields": [
...
...         ],
...         "response_data": [
...
...         ],
...         "signing_url": null,
...         "signing_redirect_url": null,
...         "final_copy_uri": "",
                "signature_id": "9c5d829af9e0566134dd28bd064a2fe8",
...         "signatures": [
...             {
...                 "signature_id": "9c5d829af9e0566134dd28bd064a2fe8",
...                 "has_pin": false,
...                 "signer_email_address": "napopasas@gmail.com",
...                 "order": null,
...                 "status_code": "awaiting_signature",
...                 "signed_at": null,
...                 "last_viewed_at": null,
...                 "last_reminded_at": null
...             }
...         ],
...         "cc_email_addresses": [
...
...         ]
...     }
... }"""
>>> import json
>>> import pprint
>>> pprint.pprint(json.loads(a))
{u'account_guid': None,
 u'client_id': u'290e6bb260fed5613b68066084fc77ff',
 u'event': {u'event_hash': u'567b6a342e29ceb8ad3a9641ccd27968f04f2c744c7dd3af8a1922f4eaa5d337',
            u'event_metadata': {u'event_message': None,
                                u'related_signature_id': None,
                                u'reported_for_account_id': None,
                                u'reported_for_app_id': u'290e6bb260fed5613b68066084fc77ff'},
            u'event_time': u'1407227830',
            u'event_type': u'signature_request_sent'},
 u'signature_request': {u'cc_email_addresses': [],
                        u'custom_fields': [],
                        u'final_copy_uri': u'',
                        u'has_error': False,
                        u'is_complete': False,
                        u'message': u'Awesome, right?',
                        u'original_title': u'My First embedded signature request with a template',
                        u'response_data': [],
                        u'signatures': [{u'has_pin': False,
                                         u'last_reminded_at': None,
                                         u'last_viewed_at': None,
                                         u'order': None,
                                         u'signature_id': u'9c5d829af9e0566134dd28bd064a2fe8',
                                         u'signed_at': None,
                                         u'signer_email_address': u'napopasas@gmail.com',
                                         u'status_code': u'awaiting_signature'}],
                        u'signing_redirect_url': None,
                        u'signing_url': None,
                        u'subject': u'My First embedded signature request with a template',
                        u'test_mode': True,
                        u'title': u'My First embedded signature request with a template'}}
>>>

答案 1 :(得分:0)

import json
str1 = '{"event": {"event_type": "signature_request_sent","event_time": "1407227830","event_hash": "567b6a342e29ceb8ad3a9641ccd27968f04f2c744c7dd3af8a1922f4eaa5d337","event_metadata": {"related_signature_id": null,"reported_for_account_id": null,"reported_for_app_id": "290e6bb260fed5613b68066084fc77ff","event_message": null}},"account_guid": null,"client_id": "290e6bb260fed5613b68066084fc77ff","signature_request": {"title": "My First embedded signature request with a template","original_title": "My First embedded signature request with a template","subject": "My First embedded signature request with a template","message": "Awesome, right?","test_mode": true,"is_complete": false,"has_error": false,"custom_fields": [],"response_data": [],"signing_url": null,"signing_redirect_url": null,"final_copy_uri": "","signatures": [{"signature_id": "9c5d829af9e0566134dd28bd064a2fe8","has_pin": false,"signer_email_address": "napopasas@gmail.com","order": null,"status_code": "awaiting_signature","signed_at": null,"last_viewed_at": null,"last_reminded_at": null}],"cc_email_addresses": []}}'
js1 = json.loads(str1)
print type(js1)

这会给你一个python dict。使用json.loads()将你的json转换为python dict。希望这对你有所帮助