1. 程式人生 > >python將xml轉換成json數據

python將xml轉換成json數據

.text ict get request class mlp 格式化 json數據 str

# -*- coding: utf-8 -*-
import requests
import xmltodict
import json

def get_response(request_url):
    r = requests.get(request_url)
    rt = r.text
    return rt

def xmlstr2jsonstr(xmlstr):
    xmlparse = xmltodict.parse(xmlstr)
   #dumps()方法的ident=1,格式化json jsonstr
= json.dumps(xmlparse,indent=1)
return jsonstr def main(request_url): xmlstr = get_response(request_url) jsonstr = xmlstr2jsonstr(xmlstr) print(jsonstr) if __name__ == __main__: request_url = ‘‘ main(request_url)

python將xml轉換成json數據