将 Json 转换为 XML - 带有数组值的 Json

Posted

技术标签:

【中文标题】将 Json 转换为 XML - 带有数组值的 Json【英文标题】:Convert Json to XML - Json with array values 【发布时间】:2020-11-20 18:58:38 【问题描述】:

我知道有很多与我类似的问题,但没有一个对我有用。 我的 json 文件有演员、导演和流派的数组。如果这个数组在构建 xml 时我很难处理。

这是json文件:

[
   
      "title":"The Kissing Booth",
      "year":"2018",
      "actors":[
         "Megan du Plessis",
         "Lincoln Pearson",
         "Caitlyn de Abrue",
         "Jack Fokkens",
         "Stephen Jennings",
         "Chloe Williams",
         "Michael Miccoli",
         "Juliet Blacher",
         "Jesse Rowan-Goldberg",
         "Chase Dallas",
         "Joey King",
         "Joel Courtney",
         "Jacob Elordi",
         "Carson White",
         "Hilton Pelser"
      ],
      "genre":[
         "Comedy",
         "Romance"
      ],
      "description":"A high school student is forced to confront her secret crush at a kissing booth.",
      "directors":[
         "Vince Marcello"
      ]
   ,
   
      "title":"Dune",
      "year":"2020",
      "actors":[
         "Rebecca Ferguson",
         "Zendaya",
         "Jason Momoa",
         "Timoth\u00e9e Chalamet",
         "Dave Bautista",
         "Josh Brolin",
         "Oscar Isaac",
         "Stellan Skarsg\u00e5rd",
         "Javier Bardem",
         "Charlotte Rampling",
         "David Dastmalchian",
         "Stephen McKinley Henderson",
         "Sharon Duncan-Brewster",
         "Chen Chang",
         "Babs Olusanmokun"
      ],
      "genre":[
         "Adventure",
         "Drama",
         "Sci-Fi"
      ],
      "description":"Feature adaptation of Frank Herbert's science fiction novel, about the son of a noble family entrusted with the protection of the most valuable asset and most vital element in the galaxy.",
      "directors":[
         "Denis Villeneuve"
      ]
   ,
   
      "title":"Parasite",
      "year":"2019",
      "actors":[
         "Kang-ho Song",
         "Sun-kyun Lee",
         "Yeo-jeong Jo",
         "Woo-sik Choi",
         "So-dam Park",
         "Jeong-eun Lee",
         "Hye-jin Jang",
         "Myeong-hoon Park",
         "Ji-so Jung",
         "Hyun-jun Jung",
         "Keun-rok Park",
         "Jeong Esuz",
         "Jo Jae-Myeong",
         "Ik-han Jung",
         "Kim Gyu Baek"
      ],
      "genre":[
         "Comedy",
         "Drama",
         "Thriller"
      ],
      "description":"Greed and class discrimination threaten the newly formed symbiotic relationship between the wealthy Park family and the destitute Kim clan.",
      "directors":[
         "Bong Joon Ho"
      ]
   ,
   
      "title":"Money Heist",
      "year":null,
      "actors":[
         "\u00darsula Corber\u00f3",
         "\u00c1lvaro Morte",
         "Itziar Itu\u00f1o",
         "Pedro Alonso",
         "Miguel Herr\u00e1n",
         "Jaime Lorente",
         "Esther Acebo",
         "Enrique Arce",
         "Darko Peric",
         "Alba Flores",
         "Fernando Soto",
         "Mario de la Rosa",
         "Juan Fern\u00e1ndez",
         "Rocco Narva",
         "Paco Tous",
         "Kiti M\u00e1nver",
         "Hovik Keuchkerian",
         "Rodrigo De la Serna",
         "Najwa Nimri",
         "Luka Peros",
         "Roberto Garcia",
         "Mar\u00eda Pedraza",
         "Fernando Cayo",
         "Antonio Cuellar Rodriguez",
         "Anna Gras",
         "Aitana Rinab Perez",
         "Olalla Hern\u00e1ndez",
         "Carlos Su\u00e1rez",
         "Mari Carmen S\u00e1nchez",
         "Antonio Romero",
         "Pep Munn\u00e9"
      ],
      "genre":[
         "Action",
         "Crime",
         "Mystery",
         "Thriller"
      ],
      "description":"An unusual group of robbers attempt to carry out the most perfect robbery in Spanish history - stealing 2.4 billion euros from the Royal Mint of Spain."
   ,
   
      "title":"The Vampire Diaries",
      "year":null,
      "actors":[
         "Paul Wesley",
         "Ian Somerhalder",
         "Kat Graham",
         "Candice King",
         "Zach Roerig",
         "Michael Trevino",
         "Nina Dobrev",
         "Steven R. McQueen",
         "Matthew Davis",
         "Michael Malarkey"
      ],
      "genre":[
         "Drama",
         "Fantasy",
         "Horror",
         "Mystery",
         "Romance",
         "Thriller"
      ],
      "description":"The lives, loves, dangers and disasters in the town, Mystic Falls, Virginia. Creatures of unspeakable horror lurk beneath this town as a teenage girl is suddenly torn between two vampire brothers."
   
]

我想将我的 json 文件转换为 xml,我的 python 代码:

import json as j
import xml.etree.ElementTree as ET

with open("imdb_movie_sample.json") as json_format_file:
    data = j.load(json_format_file)

root = ET.Element("movie")

ET.SubElement(root,"title").text = data["title"]
ET.SubElement(root,"year").text = str(data["year"])

actors = ET.SubElement(root,"actors") #.text = data["actors"]
actors.text = ''
for i in jsondata[0]['movie'][0]['actors']:
    actors.text = actors.text + '\n\t\t' + i

genre = ET.SubElement(root,"genre") #.text = data["genre"]
genre.text = ''
for i in jsondata[0]['movie'][0]['genre']:
    genre.text = genre.text + '\n\t\t' + i

ET.SubElement(root,"description").text = data["description"] 

directors = ET.SubElement(root,"directors") #.text = data["directors"]
directors.text = ''
for i in jsondata[0]['movie'][0]['directors']:
    directors.text = directors.text + '\n\t\t' + i

tree = ET.ElementTree(root)
tree.write("imdb_sample.xml")

有谁知道如何帮助我做到这一点?谢谢。

【问题讨论】:

你为什么要自己编码?有一些工具可以将 json 转换为 xml。 @Mike67 感谢您的帮助,您能告诉我一个吗? 我刚用过这个网站,xml看起来是正确的:convertjson.com/json-to-xml.htm @Mike67 谢谢!看来这里也有用。 在现成的 json-to-xml 转换器上找到您想要的 XML 实际上是很不寻常的。通常您必须进行一些后期处理,在许多情况下,这意味着您不妨自己完成整个工作。 【参考方案1】:

我在 pypi 上找到了这个。在询问其他人之前,我总是会尝试查看 pypi 以查看存在的内容。它是一个很棒的资源,包含由大量开发人员创建的 python 包。

https://pypi.org/project/json2xml/

【讨论】:

以上是关于将 Json 转换为 XML - 带有数组值的 Json的主要内容,如果未能解决你的问题,请参考以下文章

带有数组值的swift json字符串转换为[UInt8]

将 Queryset 转换为带有 JSON 值的字典

使用 Json.NET (Newtonsoft) 将空数组从 JSON 转换为 XML

带有 JSON 数组的 Jquery - 转换为 Javascript 数组

将 JSON 数组转换为 XML

如何在没有可选值的情况下将字典数组快速转换为json字符串[重复]