python实现xml与json互转

邵绪
2023-12-01
pip install xmltodict
import xmltodict
import json
dinner_recipe = '''<html><body><table>
<tr><th>amt</th><th>unit</th><th>item</th></tr>
<tr><td>24</td><td>slices</td><td>baguette</td></tr>
<tr><td>2+</td><td>tbsp</td><td>olive oil</td></tr>
<tr><td>1</td><td>cup</td><td>tomatoes</td></tr>
<tr><td>1</td><td>jar</td><td>pesto</td></tr>
</table></body></html>'''
# xml格式转json
j=json.dumps(xmltodict.parse(dinner_recipe))
print(j)
# json转xml
x=xmltodict.unparse(json.loads(j),pretty=True)
print(x)
 类似资料: