如何在Python中将XML转换为JSON

2024-06-18 18:15:29 发布

您现在位置:Python中文网/ 问答频道 /正文

Possible Duplicate:
Converting XML to JSON using Python?

我正在导入一个XML提要,并尝试将其转换为JSON进行输出。我得到了这个错误:

TypeError: <xml.dom.minidom.Document instance at 0x72787d8> is not JSON serializable

不幸的是,我对Python几乎一无所知。我正在谷歌应用引擎上开发这个。我需要一些帮助,因为我的2小时的小黑客程序进行得很顺利,现在已经是第三天了。在

XML数据:

<?xml version="1.0" ?><eveapi version="2">
  <currentTime>2009-01-25 15:03:27</currentTime>
  <result>
    <rowset columns="name,characterID,corporationName,corporationID" key="characterID" name="characters">
      <row characterID="999999" corporationID="999999" corporationName="filler data" name="someName"/>
    </rowset>
  </result>
  <cachedUntil>2009-01-25 15:04:55</cachedUntil>

</eveapi>

我的代码:

^{pr2}$

Tags: namejsonversionxmlresultduplicatepossiblecurrenttime
1条回答
网友
1楼 · 发布于 2024-06-18 18:15:29

I'm quickly coming to the opinion that Python is potentially a great language, but that none of its users know how to actually document anything in a clear and concise way.

这个问题的态度无助于从这些Python用户那里得到答案。在

正如对this related question的回答中所提到的,XML和JSON之间没有1对1的对应关系,因此转换不能自动完成。在

the documentation for ^{}中,您可以找到它能够序列化的类型列表,这些类型基本上是本机Python类型(dict、list、unicode、int、float、True/False、None)。在

因此,您必须创建一个只包含这些类型的Python数据结构,然后将其交给simplejson.dump()。在

相关问题 更多 >