通过Python将JSON转换为字典 [英] Converting JSON to Dictionary by Python

查看:107
本文介绍了通过Python将JSON转换为字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的字典:

{
    "reserve": {
        "duration": {
            "startTimeUnix": "",
            "startTime": "صبور باشید...",
            "endTime": "۱۳۹۴/۰۹/۰۴ ۱۲:۲۰",
            "endTimeUnix": 1448441400
        },
        "service": null,
        "reserver": {
            "first_name": "مریم",
            "last_name": "موسوی",
            "phone": "09124955173"
        }
    },
    "block": {
        "duration": null
    },
    "is_block": false,
    "taken_time": null,
    "staff": "alireza",
    "service": [
        "O5KLFPZB"
    ]
}

然后,我发布到Django后端服务器通过AngularJS,那么当我使用 request.POST 时,我只得到< QueryDict:{}> 我使用了 request.body ,之后我给了:

And then, I posted it to a Django backend server via AngularJS, then I just get <QueryDict: {}> when I use request.POST, so, I used request.body, after then I gave:

b'{
    "reserve": {
        "duration": {
            "startTimeUnix": "",
            "startTime": "\xd8\xb5\xd8\xa8\xd9\x88\xd8\xb1 \xd8\xa8\xd8\xa7\xd8\xb4\xdb\x8c\xd8\xaf...",
            "endTime": "\xdb\xb1\xdb\xb3\xdb\xb9\xdb\xb4/\xdb\xb0\xdb\xb9/\xdb\xb0\xdb\xb4        \xdb\xb1\xdb\xb2:\xdb\xb2\xdb\xb0",
            "endTimeUnix": 1448441400
        },
        "service": null,
        "reserver": {
            "first_name": "\xd9\x85\xd8\xb1\xdb\x8c\xd9\x85",
            "last_name": "\xd9\x85\xd9\x88\xd8\xb3\xd9\x88\xdb\x8c",
            "phone": "09124955173"
        }
    },
    "block": {
        "duration": null
    },
    "is_block": false,
    "taken_time": null,
    "staff": "alireza",
    "service": [
        "O5KLFPZB"
    ]
}'

pt

import json
from django.views.decorators.csrf import csrf_exem

@csrf_exempt
def admin_block_time(request):
    dic = json.loads(request.body.encode("utf-8"))
    print(dic)

它到Dictionary,虽然我试过 json.loads(),但它没有工作。

How can I convert it to Dictionary, although I tried json.loads(), but, it didn't work.

推荐答案

您的输入的类型为个字节,因此 json.loads(your_json)将提高 TypeError:JSON对象必须是str,而不是'bytes'

Your input is of type bytes, therefore json.loads(your_json) will raise TypeError: the JSON object must be str, not 'bytes'.

解决方案是解码它在 Content-Type HTTP头文件中指定的编码:

The solution is to decode it with the encoding specified in the Content-Type HTTP Header:

>>> import json
>>> json.loads(your_json.decode("utf-8"))
{
    'is_block': False,
    'taken_time': None,
    'staff': 'alireza',
    'block': {'duration': None},
    'service': ['O5KLFPZB'],
    'reserve': {
        'service': None,
        'duration': {
            'endTime': '۱۳۹۴/۰۹/۰۴        ۱۲:۲۰',
            'startTimeUnix': '',
            'endTimeUnix': 1448441400,
            'startTime': 'صبور باشید...'
        },
        'reserver': {
            'first_name': 'مریم',
            'phone': '09124955173',
            'last_name': 'موسوی'
        }
    }
}

这篇关于通过Python将JSON转换为字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆