最近要在客户服务器上做一个接口,需要从另一个系统拉取数据到本地,对方提供了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+"</%s>" % key
elif type(json) is list:
for l in json:
content=content+"<%s>" % key
json_to_xml("",l)
content=content+"</%s>" % key