3

xmltodict を使用して json を xml に変換するのに問題があります。単一のルートと単一のオブジェクトで正常に動作しますが、複数のオブジェクトを変換しようとすると、ValueError "ValueError: document with multiple roots" が返されます。

これが私のJSONデータです:

これまでの私のスクリプトは次のとおりです。

import json
import xmltodict

    y = """{{  "markers":[  {  "point":"new GLatLng(40.266044,-74.718479)","awayTeam":"LUGip","markerImage":"images/red.png","fixture":"Wednesday 7pm","information":"Linux users group meets second Wednesday of each month.","previousScore":"","capacity":"","homeTeam":"Lawrence Library"},{  "point":"new GLatLng(40.211600,-74.695702)","awayTeam":"LUGip HW SIG","tv":"","markerImage":"images/white.png","fixture":"Tuesday 7pm","information":"Linux users can meet the first Tuesday of the month to work out harward and configuration issues.","capacity":"","homeTeam":"Hamilton Library"},{  "point":"new GLatLng(40.294535,-74.682012)","awayTeam":"After LUPip Mtg Spot","tv":"","markerImage":"images/newcastle.png","fixture":"Wednesday whenever","information":"Some of us go there after the main LUGip meeting, drink brews, and talk.","capacity":"2 to 4 pints","homeTeam":"Applebees"}]}"""

y2 = json.loads(y)
print(xmltodict.unparse(y2, pretty = True))

結果:

Traceback (most recent call last):

  File "<ipython-input-89-8838ce8b0d7f>", line 1, in <module>
    print(xmltodict.unparse(y2,pretty=True))

  File "/Users/luzazul/anaconda/lib/python3.4/site-packages/xmltodict.py", line 323, in unparse
    raise ValueError('Document must have exactly one root.')

ValueError: Document must have exactly one root.

どんな助けでも大歓迎です、ありがとう!

4

2 に答える 2

0

xmljsonライブラリを使用して、さまざまなXML JSON 規則を使用して変換できます。要素の配列を返すことにより、複数のルートをサポートします。

例えば:

>>> import xmljson
>>> xmljson.badgerfish.etree({'x': 1, 'y': 2})
[<Element y at 0x3114a08>, <Element x at 0x3114a48>]

--xy要素の両方が返されます。

于 2015-09-20T07:56:44.490 に答える