简体   繁体   English

python json对象序列化奇怪的结果

[英]python json object serialization strange results

I am experiencing a strange serialization "effect" that I cannot figure out why it is happening. 我正在经历一个奇怪的序列化“效果”,我无法弄清楚它为什么会发生。

Essentially, one property is being represented as expected and another is not. 本质上,一个属性被表示为预期的,而另一个则没有。 For example, based on the test below I am expecting to get: {"source_system": "ABC", "target_system": "DEF"} not {"source_system": ["ABC"], "target_system": "DEF"} 例如,基于下面的测试,我期望得到:{“ source_system”:“ ABC”,“ target_system”:“ DEF”}而不是{“ source_system”:[“ ABC”],“ target_system”:“ DEF” }

Seems to think the one property source_system is a tuple but I cannot figure out why... likely I am being blind. 似乎认为一个属性source_system是一个元组,但我不知道为什么...很可能是盲目的。

I get the same result with json library as with jsonpickle as shown in the example 我在json库中得到与jsonpickle相同的结果,如示例中所示

import json
import jsonpickle

class testclass(object):
    def __init__(self,
                 _source_system = "",
                 _target_system = ""
                 ):

        self.source_system = _source_system,
        self.target_system = _target_system

    def to_JSON(self):
        return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)    
        # return jsonpickle.encode(self, unpicklable=False)   


def main():
    test = testclass(_source_system = 'ABC', _target_system='DEF')

    print(test.to_JSON())                 
    print(jsonpickle.encode(test, unpicklable=False))
    print(jsonpickle.encode(test))

#============================================================================
if __name__ == '__main__':
    main()

and the results are: 结果是:

{
    "source_system": [
        "ABC"
    ], 
    "target_system": "DEF"
}
{"source_system": ["ABC"], "target_system": "DEF"}
{"py/object": "__main__.testclass", "source_system": {"py/tuple": ["ABC"]}, "target_system": "DEF"}

Why does it think source_system is a tuple and putting it in [] list brackets ? 为什么它认为source_system是一个元组并将其放在[]列表括号中? And, why are both properties not be treated/serialized the same ? 而且,为什么两个属性都不被相同地对待/序列化?

The line 这条线

self.source_system = _source_system,

has a trailing comma, so self.source_system is a tuple. 有一个逗号结尾,所以self.source_system 一个元组。

As stated by @fimnor removing the comma inside your __init__ function should do the trick. 正如@fimnor所述,删除__init__函数中的逗号应该可以解决问题。 (May be he will make his comment in an answer, mine here just to explain.) (也许他会在回答中发表评论,在此只是为了解释。)

class testclass(object):
    def __init__(self,
             _source_system = "",
             _target_system = ""
             ):

    self.source_system = _source_system,
    self.target_system = _target_system

def to_JSON(self):
    return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)    
    # return jsonpickle.encode(self, unpicklable=False)

The comma after _source_system in your __init__ makes it a one-tuple and therefore it is converted to JavaScript array. __init__ _source_system之后的逗号使它成为一个元组,因此它被转换为JavaScript数组。 self.source_system = _source_system, is the same as self.source_system = (_source_system,) . self.source_system = _source_system,self.source_system = (_source_system,)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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