python to_json 递归
来源:互联网 发布:小米算法工程师笔试题 编辑:程序博客网 时间:2024/04/29 22:04
def f_to_json(obj,result): u""" 递归函数调用 """ if type(obj) == types.DictType: result.append("{") is_null = True for k,v in obj.items(): is_null = False result.append("'%s':"%k) f_to_json(v,result) result.append(",") if not is_null: result.pop() result.append("}") elif type(obj) == types.ListType: result.append("[") is_null = True for elem in obj: is_null = False f_to_json(elem,result) result.append(",") if not is_null: result.pop() result.append("]") elif type(obj) == types.IntType: result.append("%s"%obj) elif type(obj) == types.StringType: result.append("'%s'"%obj) return "".join(result)def stack_to_json(obj): result = [] stack = [] stack.append(obj) while stack: elem = stack.pop() if type(elem) == types.DictType: stack.append("{") is_null = True for k,v in elem.items(): is_null = False stack.append("'%s'"%k) stack.append(":") if type(v) == types.StringType: stack.append("'%s'"%v) else: stack.append(v) stack.append(",") if not is_null: stack.pop() stack.append("}") elif type(elem) == types.ListType: stack.append("[") is_null = True for e in elem: is_null = False if type(e) == types.StringType: stack.append("'%s'"%e) else: stack.append(e) stack.append(",") if not is_null: stack.pop() stack.append("]") elif type(elem) in( types.IntType,types.StringType): result.append("%s"%elem) else: print 'other',elem break result.reverse() return "".join(result) if __name__ == "__main__": r = [] d = {"a":[{"c":"v","g":"gg"}],"a2":["a","b","c",{"k":"v"}]} print 'original dict====',d print 'function to json ====',f_to_json(d,r) print 'stack to json========',stack_to_json(d)
0 0
- python to_json 递归
- rails to_json
- rails to_json
- pandas.DataFrame.to_json按行转json
- Python递归
- Python递归
- python递归
- Python | 递归
- Python 递归
- python 递归
- python 函数递归 尾递归
- Python 尾递归优化
- python递归遍历
- python 尾递归
- Python创建递归目录
- python学习~递归函数
- warning,Python 递归危险
- python 递归分析
- Jenkins集成--样式都去哪儿啦?
- Hadoop2.2.0单节点安装及测试
- 天气预报 接口数据获取
- 年终总结-2014
- 防止网页文本被复制、Ctrl C等、拽移
- python to_json 递归
- JSP页面的访问控制
- 云是物联网的基础 物联网或改变云架构
- 在qt下编写基于KUKA youbot API的程序
- 提高网站性能的方法
- jsp页面弹窗 select下拉框数据选中问题
- django 查询返回字典
- android WebView全面详解
- ofstream和ifstream详细用法