Python update object from dictionary
2012-03-06 22:07
387 查看
这两天再处理传来的json时,遇到了如何将json转换到 python object的这个问题,原来用java的时候常常直接使用gson这样方便的工具包,也就对这样的问题得过且过了
其实在python中也直接包含了比较方便的json转换工具比方说simplejson,如果在django中simplejson被更进一步得到了包装,我最初尝试直接通过 django.core.deserialize.deserialize(format,data)进行转换,但遇到了一些问题,我翻遍了django文档中这部分的 illustration ,没有找到我想要的解决方案,可是项目比较紧,于是我尝试从深层出发曲线救国,当然从json字符解析开始不太明智,于是我选择从simplejson开始。
json 的decode在simplejson中主要 通过这个方法
返回值为一个dict
e.g:
是否有一个好的方法能将dict类型迅速赋值给一个object 呢,
通过stactoverflow http://stackoverflow.com/questions/405489/python-update-object-from-dictionary
我们得到了两种解决方案
and:
前者,我经过试验,貌似有点问题,object.__dict__ 返回的是个dictproxy类型,我不知道这个是什么类型,但很显然无法使用update()这个方法。
第二种解决方案,是比较显而易见的,利用了setattr将遍历而来的值符入类型中。比较直观。
于是我采用了这种方式,不出意外的话,python或django一定提供了更为直接工具来完成json->object
我将会继续实验找出更好的便捷方式。也希望博客园各位前辈指点
java中的gson我个人觉得应该也是通过反射实现的,相比java的反射,python作为动态语言的优点通过setattr getattr已经显而易见了。。。
其实在python中也直接包含了比较方便的json转换工具比方说simplejson,如果在django中simplejson被更进一步得到了包装,我最初尝试直接通过 django.core.deserialize.deserialize(format,data)进行转换,但遇到了一些问题,我翻遍了django文档中这部分的 illustration ,没有找到我想要的解决方案,可是项目比较紧,于是我尝试从深层出发曲线救国,当然从json字符解析开始不太明智,于是我选择从simplejson开始。
json 的decode在simplejson中主要 通过这个方法
simplejson.loads()
返回值为一个dict
e.g:
data=simplejson.loads('{ "firstName": "Brett" }')
是否有一个好的方法能将dict类型迅速赋值给一个object 呢,
通过stactoverflow http://stackoverflow.com/questions/405489/python-update-object-from-dictionary
我们得到了两种解决方案
def update(self, b): self.__dict__.update(b)
and:
def update(self,newdata): for key,value in newdata: setattr(self,key,value)
前者,我经过试验,貌似有点问题,object.__dict__ 返回的是个dictproxy类型,我不知道这个是什么类型,但很显然无法使用update()这个方法。
第二种解决方案,是比较显而易见的,利用了setattr将遍历而来的值符入类型中。比较直观。
于是我采用了这种方式,不出意外的话,python或django一定提供了更为直接工具来完成json->object
我将会继续实验找出更好的便捷方式。也希望博客园各位前辈指点
java中的gson我个人觉得应该也是通过反射实现的,相比java的反射,python作为动态语言的优点通过setattr getattr已经显而易见了。。。
相关文章推荐
- 调用系统相机:[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from
- *** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from o
- Downcast from'[NSObject:AnyObject]?'to'Dictionary<NSObject,AnyObject>'only unwraps
- LINK - EF6 Update/Insert/Delete model object from outside of DbContext
- HOW TO: Update a Database from a DataSet Object Us
- Python 字典(Dictionary) update()方法
- [__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from object
- [__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from object
- Python Iteriate Dictionary Object
- Get a List of Keys From a Dictionary in Both Python 2 and Python 3
- The method update(String, Object...) from the type JdbcTemplate refers to the missing type DataAcces
- ***-[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from ob
- python update from 2.6 to 2.7
- Python 字典(Dictionary) update()方法
- 《Beginning Python From Novice to Professional》学习笔记六:Dictionary
- 解决error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No su
- JSONArray.fromObject转换不了,提示JSONArray没有fromObject方法
- MYSQL问题:You can't specify target table '表名' for update in FROM clause
- no suitable constructor found, can not deserialize from Object value (missing default constructor or
- Python中用json.loads解码字符串出错:ValueError: No JSON object could be decoded