raise TypeError(f”Object of type {type(o).__name__} is not JSON serializable”)

flask 在初步使用中遇到,is not JSON serializable 问题处理,当data 是一个python class对象,或者是一个数组的时候,总之不是基本类型时会触发,

报错的api :jsonify(data)
data = {
    "message": message,
    "code": errCode,
   # "data": “name” 没有问题
    "data": zipResultList[0] #触发is not JSON serializable 
}

解决方法:
抽一个类,统一调用__dict__方法,
ensure_ascii=False 处理中文乱码

class MyJSONEncoder(JSONEncoder):
    def default(self, obj):
        return obj.__dict__

datas = json.dumps(data, cls=MyJSONEncoder, ensure_ascii=False)
return datas
处理完后已经是json数据了,不需要再调用 flask的jsonify(data)。

49 条评论

  1. Hi There,
    .
    I hope this comment finds you well. My name is
    Mahmud Ghazni and I specialize in WordPress development and SEO services, and I wanted to get in touch to see if you might be interested in discussing how we could potentially work together.

    .
    I have a team of experienced WordPress developers and SEO
    experts who are dedicated to helping businesses like yours succeed
    online. We have a proven track record of delivering results for our clients, and we would love
    the opportunity to do the same for you.
    .
    If you are interested in learning more about my
    services, I would be happy to schedule a call or Zoom Meeting at
    a time that works for you. Alternatively, you can visit my website at ghazni.me to learn more about what I do.

    .
    I look forward to potentially working with you and helping your business grow.

    .
    Best regards,
    Mahmud Ghazni
    WhatsApp: +8801322311024
    Email: [email protected]
    .
    Note: If you are not interested, please disregard this comment.

  2. https://tinyurl.com/bdeuycjr https://tinyurl.com/4tdytrmu https://cutt.ly/8Hvc9va http://ubezpieczsieznami.pl https://tinyurl.com/2p9djedy https://bit.ly/3Mll4ge http://ubezpieczenieauta.com.pl/ https://cutt.ly/THvc0u2 https://cutt.ly/SHvc7Rb https://is.gd/9bKIGi
    https://cutt.ly/dHvvgh9 https://bit.ly/38rkKy6 https://is.gd/VZy27c https://cutt.ly/KHvcbmW https://cutt.ly/THvc0u2 https://Bit.ly/3sDZCLD https://rebrand.ly/f22a04 https://tinyurl.com/jhbkmkyt http://ubezpieczenia-dabrowa.pl https://tinyurl.com/2p9djedy
    Bit.ly https://rebrand.ly kalkulatorubezpieczen.pl https://rebrand.ly/ tinyurl.com https://tinyurl.com https://rebrand.ly/2b4251 rebrand.ly
    https://cutt.ly/hHvcg7L Tinyurl.com
    https://rebrand.ly is.gd https://tinyurl.com bit.ly tinyurl.com
    cutt.ly

  3. https://is.gd/Bgezjx https://Cutt.ly/tHvvQGp https://cutt.ly/3HvvJeP https://bit.ly/3sD5R2B https://tinyurl.com/3bk2ypnr https://cutt.ly/rHvvOSp http://ubezpieczniewroclaw.pl https://is.gd/N0bbOs https://is.gd/9bKIGi http://noweubezpieczenia.pl
    https://bit.ly/3wfG3LU https://bit.ly/3FSiwUM https://rebrand.ly/02aaad https://cutt.ly/MHvbzWH http://ubezpieczeniabb.pl http://ubezpieczeniaporeba.pl https://rebrand.ly/862cda https://bit.ly/3wfkMCe https://Cutt.ly/FHvcN2Z https://rebrand.ly/1de799
    https://tinyurl.com/ https://bit.ly/3lc6GuY ubezpieczenia-ranking.pl ubezpieczeniagdansk.com.pl https://bit.ly/3sD5R2B tinyurl.com
    tinyurl.com szybkie-ubezpieczenia.pl
    https://tinyurl.com/2p9djedy https://rebrand.ly/ tinyurl.com
    taniubezpieczyciel.pl https://tinyurl.com/mrxsbn9r ubezpieczto.pl bit.ly ubezpieczenia-czorny.pl

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注