Python,touple不是JSON可序列化的

时间:2019-07-09 12:00:22

标签: json python-3.x serialization

我有一个像这样的数据:

{
    "DATA": [
        {
            "airlines_id": 1,
            "airlines_name": "Garuda Airlines",
            "status": "1",
            "flight_no": "GA"
        },
        {
            "airlines_id": 3,
            "airlines_name": "Sriwijaya Airlines",
            "status": "1",
            "flight_no": "SJ,SA"
        },
        {
            "airlines_id": 4,
            "airlines_name": "Susi Airlines",
            "status": "1",
            "flight_no": "SI"
        },
        {
            "airlines_id": 5,
            "airlines_name": "Multi Airlines",
            "status": "1",
            "flight_no": "MA,MU,MAS"
        },
   ]
}

当我尝试做时:

mydata = {"tk_kval": "(1, \'myname\')", "tk_kgroup": "----", "tk_descr": "Execute SQL", "t_owner": 1}

我得到了错误:

  

TypeError:ProgrammingError(在“ myname”处或附近的语法错误\ nLINE 1:... cute SQL \“,\” t_owner \“:1},{\” tk_kval \“:\”(1, 'myname \')\“ ... \ n ^ \ n',)不可序列化

我认为问题是(1,\'myname \')乱七八糟,所以我创建了一个像这样的类:

json.dumps(mydata)

然后我这样做:

class MultiDimensionalArrayEncoder(json.JSONEncoder):
    def encode(self, obj):
        def hint_tuples(item):
            if isinstance(item, tuple):
                return {'__tuple__': True, 'items': item}
            if isinstance(item, list):
                return [hint_tuples(e) for e in item]
            if isinstance(item, dict):
                return {key: hint_tuples(value) for key, value in item.items()}
            else:
                return item

        return super(MultiDimensionalArrayEncoder, self).encode(hint_tuples(obj))

def hinted_tuple_hook(obj):
    if '__tuple__' in obj:
        return tuple(obj['items'])
    else:
        return obj

但是我有同样的错误:

  

TypeError:ProgrammingError(在“ myname”处或附近的语法错误\ nLINE 1:... cute SQL \“,\” t_owner \“:1},{\” tk_kval \“:\”(1, 'myname \')\“ ... \ n ^ \ n',)不可序列化

有人可以帮助我序列化我的伴娘吗?

非常感谢

0 个答案:

没有答案