简体   繁体   English

无法使用jsonpickle将json字符串解码为python对象

[英]Unable to decode a json string to a python object using jsonpickle

My class structure is as follows - 我的班级结构如下 -

class HelloWorld (object):
    def __init__(self, name, i, id):
        self.name = name
        self.i = i
        self.id = id

The I'm creating an object 我在创造一个物体

p = HelloWorld('pj', 3456, '1234')

and passing this object to a definition, where I use jsonpickle.encode and jsonpickle.decode as follows 并将此对象传递给定义,其中我使用jsonpickle.encodejsonpickle.decode如下

>>>print(type(p)) 
    <class 'HelloWorld'>
 >>>x = jsonpickle.encode(p)        
 >>>print(x)
    {"i": 3456, "name": "pj", "py/object": "__builtin__.HelloWorld", "id": "1234"}
 >>>y = jsonpickle.decode(x)
 >>>print(type(y))
    <class 'dict'>

I don't understand why I'm not able to decode it back to the original object, even though the distinguishing py/object is also present. 我不明白为什么我无法将它解码回原始对象,即使区别py/object也存在。

Can anyone please suggest what am I doing wrong? 任何人都可以建议我做错了什么?

Adding the code which generate's the dynamic class for above mentioned use case. 添加生成上述用例的动态类的代码。

def _create_pojo(self, pojo_class_name, param_schema):

    #extracting the properties from the parameter 'schema'        
    pojo_properties = param_schema['properties']

    #creating a list of property keys
    pojo_property_list = []
    for key in pojo_properties:
        pojo_property_list.append(key)

    source = \
        "class %s (object):\n" % ( pojo_class_name )       
    source += "    def __init__(self, %s" % ', '.join(pojo_property_list) +"):\n" #defining __init__ for the class
    for prop in pojo_property_list: #creating a variable in the __init__ method for each property in the property list
        source += "        self.%s = %s\n" % (prop, prop)

    #generating the class code
    class_code = compile( source, "<generated class>", "exec" )
    fakeglobals = {}
    eval( class_code, {"object" : object}, fakeglobals )
    result = fakeglobals[ pojo_class_name ]        
    result ._source = source
    pprint.pprint(source)       

    self._object_types[ pojo_class_name ] = result
    return result

The main problem here is, the class is not available while decoding, hence unable to decode it as an python object. 这里的主要问题是,类在解码时不可用,因此无法将其解码为python对象。

According to the JsonPickle documentation( http://jsonpickle.github.io/#module-jsonpickle ), the object must be accessible globally via a module and must inherit from object (AKA new-style classes). 根据JsonPickle文档( http://jsonpickle.github.io/#module-jsonpickle ),对象必须可以通过模块全局访问,并且必须从对象继承(AKA新式类)。

Hence followed following recipe, http://code.activestate.com/recipes/82234-importing-a-dynamically-generated-module/ . 因此遵循以下配方, http://code.activestate.com/recipes/82234-importing-a-dynamically-generated-module/ Using this you can generate dynamic module and load the classes dynamically. 使用此方法,您可以生成动态模块并动态加载类。 In this way while decoding the class will be accessible, and hence you can decode it back to python object. 通过这种方式解码类将是可访问的,因此您可以将其解码回python对象。

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

相关问题 使用python jsonpickle创建嵌套的json对象 - Create nested json object using python jsonpickle Python:无法使用 jsonpickle 打印相同的 object 两次或更多次 - Python: unable to print the same object twice or more using jsonpickle 无法使用python解码此字符串 - unable to decode this string using python 使用jsonpickle从python中的json序列化中排除字段 - excluding fields from json serialization in python using jsonpickle Python:从一个类创建 jsonpickle 并解包,错误 AttributeError: type object &#39;&#39; has no attribute &#39;decode&#39; - Python: create jsonpickle from a class and unpack, Error AttributeError: type object ' ' has no attribute 'decode' 将JSON解码为python对象 - Decode JSON into a python object Python 无法解码字节串 - Python unable to decode byte string Runtime.ImportModuleError: Unable to import module &#39;testsdk&#39;: No module named &#39;jsonpickle&#39; with Python script using AWS Lambda - Runtime.ImportModuleError: Unable to import module 'testsdk': No module named 'jsonpickle' with Python script using AWS Lambda 在python中使用jsonpickle解析多个子属性 - Using jsonpickle in python to parse multiple child attributes 无法为Python中的Falcon测试解码JSON正文 - Unable to decode JSON body for Falcon tests in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM