json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 1

最近用postman调用接口时候报错 json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 1 (char 3)

在这里插入图片描述

如下图所示:

在这里插入图片描述
原因:这个json是从微信复制过来粘贴到postman上的,微信粘贴过来的不是标准的json格式
解决这个问题最简单的方式是,点击下postman右边的beautify,重新测试ok

在这里插入图片描述

def test(request):
    if request.method == "POST":
        if re.search(r"application/json", request.environ.get("CONTENT_TYPE")):
            # data = eval(str(request.body).replace(r"\xc2\xa0", ""))
            data = request.body
            # try:
            #     json.loads(data)
            # except Exception as e:
            #     return HttpResponse("传入的格式不是json:{}".format(e.__str__()))
            print(data)
            name = json.loads(data).get("name")
            age = json.loads(data).get("age")
            print(name, age)
        else:
            print("数据不是json格式")
            return HttpResponse("数据不是json格式")

        return HttpResponse("ok")
    else:
        return HttpResponse("请求方式不是post")

将报错的request.body打印出来
b’{\r\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0"name":\xc2\xa0"zzz",\r\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0"age":\xc2\xa019\r\n}’
下面是正常的的request.body
b’{\r\n “name”: “zzz”,\r\n “age”: 19\r\n}’
可以看到有很大的差别,key前面有很多\xc2\xa0
这个神秘字符的编码就是\xc2\xa0,上网查到这是一个叫做Non-breaking space的东西,用于阻止在此处自动换行和阻止多个空格被压缩成一个
解决方式就是把这几个神秘的字符替换成空格
str(request.body).replace(r"\xc2\xa0", “”)

def test(request):
    if request.method == "POST":
        if re.search(r"application/json", request.environ.get("CONTENT_TYPE")):
            data = eval(str(request.body).replace(r"\xc2\xa0", ""))
            # data = request.body
            # try:
            #     json.loads(data)
            # except Exception as e:
            #     return HttpResponse("传入的格式不是json:{}".format(e.__str__()))
            print(data)
            name = json.loads(data).get("name")
            age = json.loads(data).get("age")
            print(name, age)
        else:
            print("数据不是json格式")
            return HttpResponse("数据不是json格式")

        return HttpResponse("ok")
    else:
        return HttpResponse("请求方式不是post")

postman测试OK
在这里插入图片描述
当然你也可以不转换,判断是是否可以json.loads(request.body)
不是的话就抛出异常

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]、\[2\]和\[3\]中提到了相同的错误信息:json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes。这个错误通常发生在解析JSON数据时,表示JSON数据中的属性名没有用双引号括起来。在JSON中,属性名必须用双引号括起来,而不是单引号或者没有引号。 解决这个问题的方法是确保JSON数据中的属性名都被正确地用双引号括起来。你可以检查你的JSON数据,特别是在出错的行和列位置,看看是否有属性名没有被正确地用双引号括起来。 另外,引用\[3\]中提到了另一个错误信息:ValueError: dictionary update sequence element #0 has length 1; 2 is required。这个错误通常发生在将字符串的字典转换为字典时,表示字符串的字典格式不正确。你可以使用eval方法将字符串的字典转换为字典对象,然后再使用json.dumps方法将其序列化为JSON格式的数据。 综上所述,要解决json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes错误,你需要确保JSON数据中的属性名都被正确地用双引号括起来。而要解决ValueError: dictionary update sequence element #0 has length 1; 2 is required错误,你可以使用eval方法将字符串的字典转换为字典对象,然后再使用json.dumps方法将其序列化为JSON格式的数据。 #### 引用[.reference_title] - *1* [json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 40](https://blog.csdn.net/LiXueFu727224204/article/details/128943637)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 1](https://blog.csdn.net/weixin_42257924/article/details/124616189)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [生产环境json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line x ...](https://blog.csdn.net/qq_43224174/article/details/127671272)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值