猿问

(Django、Python)如何修复 json.decoder.JSONDecodeError?

我一直在尝试为三明治店制作订单/购物车应用程序。在测试时,我不断收到与解码前端工程师发送给我的 JSON 数据有关的错误。


下面是服务器收到的 POST 请求。


{

    "default_ingredients": [

        {

            "id": 1,

            "name": "이탈리안 화이트 (top)",

            "image_url": "https://media.subway.com/digital/Account_Updates/Assets/App-Base/Web_Images/Subway/en-us/Options/o_BreadItalian_customizer_large.png",

            "price": "0.00",

            "ingredient_category_id": 1

        },

        {

            "id": 23,

            "name": "토마토",

            "image_url": "https://media.subway.com/digital/Account_Updates/Assets/App-Base/Web_Images/Subway/en-us/OptionsIds/10133_customizer_large.png",

            "price": "0.00",

            "ingredient_category_id": 3

        },

    ],

    "added_ingredients": [

        {

            "id": 18,

            "name": "살라미",

            "image_url": "https://media.subway.com/digital/Account_Updates/Assets/App-Base/Web_Images/Subway/en-us/Options/o_TurkeyBasedHamSalamiBologna_customizer_large.png",

            "price": "0.00",

            "ingredient_category_id": 2

        },

        {

            "id": 19,

            "name": "페퍼로니",

            "image_url": "https://media.subway.com/digital/Account_Updates/Assets/App-Base/Web_Images/Subway/en-us/Options/o_Pepperoni_customizer_large.png",

            "price": "1800.00",

            "ingredient_category_id": 2

        },

    ],

    "product_name": "이탈리안 비엠티",

}

下面是我的订单应用程序的 views.py


繁华开满天机
浏览 158回答 1
1回答

慕田峪4524236

我找到了答案。答案是在使用 ast 评估之前用 'utf-8' 解码。body = ast.literal_eval(request.body.decode('utf-8'))
随时随地看视频慕课网APP

相关分类

Python
我要回答