栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python实现json转XML

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python实现json转XML

最近要在客户服务器上做一个接口,需要从另一个系统拉取数据到本地,对方提供了resftful接口服务,返回是json数据,需要转换为xml。但是基于客户服务器是linux系统,并且无法连外网,linux默认安装了2.7.x版本的python,便手动实现了一个python函数,不用在服务器上安装第三方依赖

#!/usr/bin/python
# -*- coding: UTF-8 -*-
content=""
def json_to_xml(key, json):
        global content
        if type(json) is dict:
          if key != "":
                content=content+"<%s>" % key
          for key1, value in json.items():
                json_to_xml(key1, value)
          if key !="":
                content=content+"" % key
        elif  type(json) is list:
            for l in json:
                content=content+"<%s>" % key
                json_to_xml("",l)
                content=content+"" % key
        else:
            content=content+"<%s>%s" % (key, json, key)
json = {"data":[{"id":"34534534","num":"20180509"},{"id":"34534534","num":"20180509"}],"code":200,"message":"ok"}
#rec("root", json)
json_to_xml("root",json)
print content
 

打印出结果:ok20020180509345345342018050934534534

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/744311.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号