简体   繁体   中英

How to print own class with jsonify in flask?

I create class:

class My_class:
    def __init__(self, List):
        self.lst = List

    def __str__(self):
        return '[' + ', '.join(str(e) for e in self.lst) + ']'

But it doesn't work when I try to print jsonify with My_class This code doesn't work:

from flask import jsonify

def Print():
   return jsonify({"list": My_class([6, 9])})

I need to print My_class without ""

How to solve this?

Surround My_class([6, 9]) with str() as the class itself isn't JSON serializable.

import json

def Print():
    return flask.jsonify({"list": json.loads(str(My_class([6, 9])))})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM