我有两个文件:
请参阅文章末尾的最小XML示例。
注意:有效转换为csv作为json的替代方案是可以接受的。 更新 - 找到另一个命令行实用程序,它可以帮助此XMLStarlet {/ 3}} {/ 3}}。{/}
我尝试过来自SO answer hay的多个工具xml2json和基本跟在xmlutils for Python的Crack gem:
require "rubygems"
require "crack"
require "json"
require 'pp'
myXML = Crack::XML.parse(File.read("20141004Flemington_5start.xml"))
myJSON = myXML.to_json
final = JSON.parse(myJSON,:symbolize_names => true)
pp final
File.write('test2.json', final)
然而,在每种情况下都会出现同样的问题。只要XML具有以下语法格式:
<Meeting MeetCode="859382" InputFilename="" GenerationDate="2014-12-05T10:09:53+11:00">
<RequestInfo RequestCode="" RequestId="0"/>
在这种情况下<Meeting />
内的所有值都将丢失。转换为JSON时如何保留我的值?
<json type="object">
<Meeting type="object">
<RequestInfo type="object">
最小的XML示例。
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="stylesheet5.xsl"?>
<Meeting MeetCode="859382" InputFilename="" GenerationDate="2014-12-05T10:09:53+11:00">
<RequestInfo RequestCode="" RequestId="0" />
<CodeType>GALLOPS</CodeType>
<MeetingCategory>Professional</MeetingCategory>
<MeetingStage>Acceptances</MeetingStage>
<MeetingStageEncoded>15</MeetingStageEncoded>
<MeetingPhase>ResultsConfirmed</MeetingPhase>
<PhaseMeetingEncoded>63</PhaseMeetingEncoded>
<NominationsClose>2014-09-29T12:00:00+10:00</NominationsClose>
<AcceptanceClose>2014-10-01T10:00:00+10:00</AcceptanceClose>
<RidersClose>2014-10-01T13:00:00+10:00</RidersClose>
<WeightsPublishing>2014-09-30T00:00:00+10:00</WeightsPublishing>
<Club ClubCode="10018" Title="Victoria Racing Club" AbrClubDesc="VRC" State="VIC" Category="Metro" />
<Track VenueName="Flemington" VenueDesc="Flemington" VenueAbbr="FLEM" VenueCode="151" TrackName="Main" TrackCode="149">
<TrackRating NumericTrackRating="3">Good</TrackRating>
<Penetrometer>4.83</Penetrometer>
<RailPosition>+9m Entire Circuit</RailPosition>
<RailPositionLastMeeting>True Position Entire Circuit</RailPositionLastMeeting>
<Weather>Fine</Weather>
<TrackSurface>Turf</TrackSurface>
<Comments>Finalised 4/10 - 7:45am Late Scratching Race 3 No. 4</Comments>
<Irrigation>Nil last 24hrs, 25mm last 7 days</Irrigation>
<Rainfall>Nil last 24hrs, 4.2mm last 7 days</Rainfall>
<RacingDirection>AntiClockwise</RacingDirection>
<TrackInfo>Penetrometer: Inside 4.85, Outside 4.85</TrackInfo>
</Track>
<MeetingType>Metro</MeetingType>
<ApprenticeMeetingType>Metro</ApprenticeMeetingType>
<MeetDate>2014-10-04</MeetDate>
<StateDesc>VIC</StateDesc>
<NumOfRaces>9</NumOfRaces>
<DayNight>Day</DayNight>
<TabStatus>true</TabStatus>
<Logos />
<XMLSchemaVersion>RISA XML 5.1</XMLSchemaVersion>
<XMLSchemaVersion>RISA XML 5.1</XMLSchemaVersion>
<Races>
<Race RaceCode="5038498" CurrentRaceStage="Results" RaceNumber="1">
<NameRaceFull StakesGroupId="346">Greenland Australia Maribyrnong Trial Stakes</NameRaceFull>
<NameRaceForm>MARIBYRNONG TRL</NameRaceForm>
<NameRaceNews>Maribyrnong Trial Stakes</NameRaceNews>
<Logos>
<Logo Id="VOBIS" Reason="Bonus Scheme" Description="Super Vobis Bonus Scheme" Image="VOBIS.png" />
</Logos>
<RaceStartTime TimeAtVenue="2014-10-04T12:40:00" AsUtc="2014-10-04T02:40:00" />
<ClosingTimes UtcOffset="10:00:00.000" Nominations="2014-09-29T12:00:00" LateNominations="2014-09-29T15:00:00" WeightsPublishing="2014-09-30T00:00:00" AcceptanceFinal="2014-10-01T10:00:00" Riders="2014-10-01T13:00:00" Scratching="2014-10-04T07:30:00" />
<NominationsRaceNumber>1</NominationsRaceNumber>
<NominationsDivisor>0</NominationsDivisor>
<GroupType>Listed</GroupType>
<RaceType>Flat</RaceType>
<EntryConditions>
<EntryCondition Type="Class" Id="~" Code="9" Short="" Medium="No class restriction" Long="No class restriction" />
<EntryCondition Type="Weight" Id="SW" Code="50" Short="SW" Medium="Set Weights" Long="Set Weights" />
<EntryCondition Type="Age" Id="2" Code="33" Short="2Y" Medium="2-Y-O" Long="Two-Years-Old" />
<EntryCondition Type="Sex" Id="~" Code="0" Short="" Medium="No sex restriction" Long="No sex restriction" />
<EntryCondition Type="Ballot" Id="BC7" Code="430001" Short="2YO SW" Medium="Early Season 2YO" Long="Selected Early Season 2YO" />
</EntryConditions>
<ApprenticeCanClaim>false</ApprenticeCanClaim>
<PrizeMoneyDetails TotalExcludingBonuses="120000.00">
<Prizes>
<PrizeMoney Position="1" Value="72000.00" />
<PrizeMoney Position="2" Value="21600.00" />
<PrizeMoney Position="3" Value="10800.00" />
<PrizeMoney Position="4" Value="5400.00" />
<PrizeMoney Position="5" Value="3000.00" />
<PrizeMoney Position="6" Value="2400.00" />
<PrizeMoney Position="7" Value="2400.00" />
<PrizeMoney Position="8" Value="2400.00" />
</Prizes>
<Bonuses>
<BonusDistribution BonusCategoryId="SVG" BonusCategoryCode="6" Scheme="VOBIS" Category="VOBIS - Group/Listed" LongDescription="This race carries a SUPER VOBIS Nominators Bonus of $10,000 for qualified horses." TotalBonus="10000.00" />
</Bonuses>
<Trophies>
<Trophy Recipient="Owner" Position="1" Value="1000.00" />
</Trophies>
<StarterSubsidy>0</StarterSubsidy>
</PrizeMoneyDetails>
<RaceDistance>1000</RaceDistance>
<DistanceApprox>false</DistanceApprox>
<TrackStraight>448</TrackStraight>
<TrackCircumference>2313</TrackCircumference>
<TrackRecords>
<TrackRecord TrackCode="149" RecordCode="436169" RaceCode="5029333">
<RaceDate>2013-02-16</RaceDate>
<RaceNumber>7</RaceNumber>
<DistanceRace>1000</DistanceRace>
<Time>00:00:55.420</Time>
<TrackRecordHorse HorseCode="790716" HorseName="Black Caviar" />
</TrackRecord>
</TrackRecords>
<SizeField>16</SizeField>
<SizeEmergency>4</SizeEmergency>
<Accepters>12</Accepters>
<Starters>11</Starters>
<WeightChange>0.00</WeightChange>
<RaceEntries>
<RaceEntry RaceEntryCode="9153413">
<Horse HorseCode="986313" HorseName="Apoloboom" Colour="BAY" Sex="C" Age="2" NamePrev="" HorseStatus="Active" HasStableReturn="true" FoalDate="2012-08-12" />
<!--StableReturnCode = 5139732-->
<TabNumber>1</TabNumber>
<RaceEntryStatusEncoded>67601</RaceEntryStatusEncoded>
<HorseOwnership>
<OfficialHorseOwners>Boom Racing 4 (Mgr J L Travaglia)</OfficialHorseOwners>
</HorseOwnership>
<Trainer TrainerCode="11156">
<Name>Robbie Laing</Name>
<Location>Cranbourne</Location>
<State>VIC</State>
<Postcode>3977</Postcode>
<Title>Mr</Title>
<PreferredName>Robbie</PreferredName>
<Surname>Laing</Surname>
<Initials>R E</Initials>
</Trainer>
<Breeding>
<Sire HorseCode="726892" HorseName="Kaphero" />
<Dam HorseCode="752264" HorseName="Apologies" />
<SireOfDam HorseCode="604480" Country="USA" HorseName="Giant's Causeway" />
<Breeder>Burnewang North Pastoral</Breeder>
</Breeding>
<JockeyRaceEntry JockeyCode="25791" Apprentice="false">
<Name>Vlad Duric</Name>
<Location>Pakenham</Location>
<State>VIC</State>
<Postcode>3810</Postcode>
<PreferredName>Vlad</PreferredName>
<Surname>Duric</Surname>
<Initials>V</Initials>
<RidingWeight>53.00</RidingWeight>
</JockeyRaceEntry>
<RacingColours>Purple, Yellow V, Purple And Yellow Diagonal Striped Sleeves, Yellow And Purple Stars Cap</RacingColours>
<BallotSequence>7=</BallotSequence>
<BarrierNumber>5</BarrierNumber>
<HandicapWeight>57.00</HandicapWeight>
<HandicapRating>0.00</HandicapRating>
<WeightPenalty>0.0</WeightPenalty>
<Gear Blinkers="true" />
<BonusIndicator SchemeName="Super Vobis">N</BonusIndicator>
<Logos>
<Logo Id="VOBIS" Reason="Bonus Scheme" Description="Super Vobis Bonus Scheme" Image="VOBIS.png" />
</Logos>
<Form>
<FormSummary />
<ResultsSummaries>
<ResultsSummary Name="TotalResults" Starts="1" Wins="0" Seconds="0" Thirds="0" PrizeMoney="0.00" PrizeMoneyExBonus="0.00" Bonus="0.00" />
<ResultsSummary Name="FirstUp" Starts="1" Wins="0" Seconds="0" Thirds="0" PrizeMoney="0.00" />
<ResultsSummary Name="SecondUp" />
<ResultsSummary Name="AtThisDistance" Starts="1" Wins="0" Seconds="0" Thirds="0" PrizeMoney="0.00" />
<ResultsSummary Name="ThisTrack" Starts="1" Wins="0" Seconds="0" Thirds="0" PrizeMoney="0.00" />
<ResultsSummary Name="TrackAndDistance" Starts="1" Wins="0" Seconds="0" Thirds="0" PrizeMoney="0.00" />
<ResultsSummary Name="Firm" />
<ResultsSummary Name="Good" Starts="1" Wins="0" Seconds="0" Thirds="0" PrizeMoney="0.00" />
<ResultsSummary Name="Soft" />
<ResultsSummary Name="Heavy" />
<ResultsSummary Name="Synthetic" />
<ResultsSummary Name="Wet" />
<ResultsSummary Name="NonWet" Starts="1" Wins="0" Seconds="0" Thirds="0" PrizeMoney="0.00" />
<ResultsSummary Name="Night" />
<ResultsSummary Name="JumpsRecord" />
<ResultsSummary Name="ThisSeason" Starts="1" Wins="0" Seconds="0" Thirds="0" PrizeMoney="0.00" />
</ResultsSummaries>
<LastStartsSummary>9</LastStartsSummary>
<LastStarts>
<HorseRaceSummary MtgDate="2014-10-04" VenueName="Flemington" VenueCode="151" AbrVenue="FLEM" RaceNumber="1" RaceCode="5038498">
<NameRaceNews>Maribyrnong Trial Stakes</NameRaceNews>
<NameRaceForm>MARIBYRNONG TRL</NameRaceForm>
<RaceDistance>1000</RaceDistance>
<GroupType>LR</GroupType>
<RaceType>Flat</RaceType>
<EntryConditions>
<EntryCondition Type="Class" Id="~" Code="9" Short="" Medium="No class restriction" Long="No class restriction" />
<EntryCondition Type="Weight" Id="SW" Code="50" Short="SW" Medium="Set Weights" Long="Set Weights" />
<EntryCondition Type="Age" Id="2" Code="33" Short="2Y" Medium="2-Y-O" Long="Two-Years-Old" />
<EntryCondition Type="Sex" Id="~" Code="0" Short="" Medium="No sex restriction" Long="No sex restriction" />
</EntryConditions>
<MeetingCategory>Professional</MeetingCategory>
<DayNight>Day</DayNight>
<ClubCode>10018</ClubCode>
<ClubCategory>Metro</ClubCategory>
<VenueCategory>Metro</VenueCategory>
<RaceStarters>11</RaceStarters>
<FinishPosition BeatenMargin="8.80" OfficialMargin="1/2 Neck">9</FinishPosition>
<MoneyRaceGrandTotal>121000.00</MoneyRaceGrandTotal>
<MoneyPrize>0.00</MoneyPrize>
<MoneyBonus>0.00</MoneyBonus>
<Jockey JockeyCode="25791" Apprentice="false">
<Name>Vlad Duric</Name>
<Location>Pakenham</Location>
<State>VIC</State>
<Postcode>3810</Postcode>
<PreferredName>Vlad</PreferredName>
<Surname>Duric</Surname>
<Initials>V</Initials>
</Jockey>
<BarrierNumber>5</BarrierNumber>
<Handicap>57.00</Handicap>
<StartingWinPrice>$13</StartingWinPrice>
<BettingFluctuations PriceOpen="$11" PriceMove1="$13" />
<OtherRunners>
<OtherRunner FinishPosition="1" Time="00:00:57.960" HorseCode="985390" HorseName="Mihalic" WinningMargin="8.80" Weight="55.00" WeightCarried="55.00" JockeyCode="22573" JockeyName="M J Zahra" Barrier="7" />
<OtherRunner FinishPosition="2" HorseCode="983001" HorseName="Iceflow" BeatenMargin="0.75" Weight="57.00" WeightCarried="57.00" JockeyCode="793032" JockeyName="D M Lane" Barrier="9" />
<OtherRunner FinishPosition="3" HorseCode="989316" HorseName="Lake Jackson" BeatenMargin="2.25" Weight="57.00" WeightCarried="57.00" JockeyCode="488500" JockeyName="M J Walker" Barrier="8" />
</OtherRunners>
<TrackCondition NumericTrackRating="3">Good</TrackCondition>
<HandicapRatingPreRace>0.00</HandicapRatingPreRace>
<WeightCarried>57.00</WeightCarried>
<InRunning Distance="800" Position="5" />
<InRunning Distance="400" Position="8" />
<DistanceSectional>600</DistanceSectional>
<TimeSectional>00:00:33.820</TimeSectional>
</HorseRaceSummary>
</LastStarts>
<AtDistance />
</Form>
<LastGear GearType="Blinkers ON" GearDate="2014-09-29" />
</RaceEntry>
</RaceEntries>
<BallotedOutEntries />
<Comments />
</Race>
</Races>
</Meeting>
答案 0 :(得分:2)
如果 Python 中安装了 lxml ,您可以编写一个使用 lxml.objectify 和 Python 字典保留值(包括日期/日期时间对象),这是一个简单的函数,可以成功转换xml样本并按要求保留所有值:
from lxml import objectify
import json
def xml_to_json(f_xml, f_json, indent=4):
def tree_to_dict(tree, tree_dict):
_dict = {}
if tree.attrib:
# store attrib in dictionary
_dict.update(tree.attrib)
elif tree.text:
_dict = tree.text
# loop through all the children nodes
for node in tree.iterchildren():
node_dict, node_list, _d = {}, [], {}
# if nested, do a recursion
node_dict = tree_to_dict(node, _d)
if not node.tag in _dict:
_dict.update(node_dict)
continue
# use list to append all same name's children node
if isinstance(_dict[node.tag], list):
_dict[node.tag].append(node_dict[node.tag])
continue
node_list.append(_dict[node.tag])
_dict[node.tag] = node_list
_dict[node.tag].append(node_dict[node.tag])
if not _dict:
_dict = None
tree_dict[tree.tag] = _dict
return tree_dict
# parse your xml file here, getroot() return the root element
root = objectify.parse(f_xml).getroot()
root_dict = {}
# make use of json module and parse the result dictionary to file
with open(f_json, 'w') as f:
json.dump(tree_to_dict(root, root_dict), f, indent=indent)
用法:
# xml input file path, json output file path, custom indent for json
xml_to_json('sample.xml', 'sample.json', indent=2)
结果:
$ cat sample.json
{
"Meeting": {
"MeetingCategory": "Professional",
"Logos": null,
"NumOfRaces": "9",
"RequestInfo": {
"RequestCode": "",
"RequestId": "0"
},
"WeightsPublishing": "2014-09-30T00:00:00+10:00",
"InputFilename": "",
"PhaseMeetingEncoded": "63",
"MeetingPhase": "ResultsConfirmed",
"RidersClose": "2014-10-01T13:00:00+10:00",
"NominationsClose": "2014-09-29T12:00:00+10:00",
"CodeType": "GALLOPS",
"MeetingType": "Metro",
"MeetDate": "2014-10-04",
"TabStatus": "true",
"XMLSchemaVersion": [
"RISA XML 5.1",
"RISA XML 5.1"
],
"Track": {
"Rainfall": "Nil last 24hrs, 4.2mm last 7 days",
"Irrigation": "Nil last 24hrs, 25mm last 7 days",
"TrackName": "Main",
"VenueName": "Flemington",
"TrackSurface": "Turf",
"TrackCode": "149",
"Comments": "Finalised 4/10 - 7:45am Late Scratching Race 3 No. 4",
"Weather": "Fine",
"Penetrometer": "4.83",
"RailPositionLastMeeting": "True Position Entire Circuit",
"RailPosition": "+9m Entire Circuit",
"VenueAbbr": "FLEM",
"TrackInfo": "Penetrometer: Inside 4.85, Outside 4.85",
"TrackRating": {
"NumericTrackRating": "3"
},
"VenueDesc": "Flemington",
"RacingDirection": "AntiClockwise",
"VenueCode": "151"
},
"StateDesc": "VIC",
"MeetingStage": "Acceptances",
"Club": {
"Category": "Metro",
"AbrClubDesc": "VRC",
"State": "VIC",
"ClubCode": "10018",
"Title": "Victoria Racing Club"
},
"Races": {
"Race": {
"Logos": {
"Logo": {
"Image": "VOBIS.png",
"Reason": "Bonus Scheme",
"Id": "VOBIS",
"Description": "Super Vobis Bonus Scheme"
}
},
"CurrentRaceStage": "Results",
"TrackCircumference": "2313",
"NameRaceNews": "Maribyrnong Trial Stakes",
"Comments": null,
"NominationsDivisor": "0",
"Starters": "11",
"TrackRecords": {
"TrackRecord": {
"TrackRecordHorse": {
"HorseName": "Black Caviar",
"HorseCode": "790716"
},
"TrackCode": "149",
"RaceCode": "5029333",
"RecordCode": "436169",
"Time": "00:00:55.420",
"RaceNumber": "7",
"RaceDate": "2013-02-16",
"DistanceRace": "1000"
}
},
"RaceCode": "5038498",
"Accepters": "12",
"RaceDistance": "1000",
"PrizeMoneyDetails": {
"StarterSubsidy": "0",
"Bonuses": {
"BonusDistribution": {
"Category": "VOBIS - Group/Listed",
"BonusCategoryCode": "6",
"LongDescription": "This race carries a SUPER VOBIS Nominators Bonus of $10,000 for qualified horses.",
"TotalBonus": "10000.00",
"Scheme": "VOBIS",
"BonusCategoryId": "SVG"
}
},
"TotalExcludingBonuses": "120000.00",
"Prizes": {
"PrizeMoney": [
{
"Position": "1",
"Value": "72000.00"
},
{
"Position": "2",
"Value": "21600.00"
},
{
"Position": "3",
"Value": "10800.00"
},
{
"Position": "4",
"Value": "5400.00"
},
{
"Position": "5",
"Value": "3000.00"
},
{
"Position": "6",
"Value": "2400.00"
},
{
"Position": "7",
"Value": "2400.00"
},
{
"Position": "8",
"Value": "2400.00"
}
]
},
"Trophies": {
"Trophy": {
"Position": "1",
"Recipient": "Owner",
"Value": "1000.00"
}
}
},
"NominationsRaceNumber": "1",
"ApprenticeCanClaim": "false",
"GroupType": "Listed",
"NameRaceFull": {
"StakesGroupId": "346"
},
"SizeField": "16",
"NameRaceForm": "MARIBYRNONG TRL",
"RaceType": "Flat",
"RaceEntries": {
"RaceEntry": {
"comment": null,
"Trainer": {
"State": "VIC",
"PreferredName": "Robbie",
"Surname": "Laing",
"Location": "Cranbourne",
"Initials": "R E",
"TrainerCode": "11156",
"Name": "Robbie Laing",
"Title": "Mr",
"Postcode": "3977"
},
"Horse": {
"HorseCode": "986313",
"Age": "2",
"FoalDate": "2012-08-12",
"HorseStatus": "Active",
"Sex": "C",
"NamePrev": "",
"HorseName": "Apoloboom",
"HasStableReturn": "true",
"Colour": "BAY"
},
"BarrierNumber": "5",
"Gear": {
"Blinkers": "true"
},
"Form": {
"LastStartsSummary": "9",
"AtDistance": null,
"FormSummary": null,
"ResultsSummaries": {
"ResultsSummary": [
{
"Bonus": "0.00",
"Name": "TotalResults",
"PrizeMoney": "0.00",
"Seconds": "0",
"Starts": "1",
"PrizeMoneyExBonus": "0.00",
"Wins": "0",
"Thirds": "0"
},
{
"Name": "FirstUp",
"PrizeMoney": "0.00",
"Seconds": "0",
"Starts": "1",
"Wins": "0",
"Thirds": "0"
},
{
"Name": "SecondUp"
},
{
"Name": "AtThisDistance",
"PrizeMoney": "0.00",
"Seconds": "0",
"Starts": "1",
"Wins": "0",
"Thirds": "0"
},
{
"Name": "ThisTrack",
"PrizeMoney": "0.00",
"Seconds": "0",
"Starts": "1",
"Wins": "0",
"Thirds": "0"
},
{
"Name": "TrackAndDistance",
"PrizeMoney": "0.00",
"Seconds": "0",
"Starts": "1",
"Wins": "0",
"Thirds": "0"
},
{
"Name": "Firm"
},
{
"Name": "Good",
"PrizeMoney": "0.00",
"Seconds": "0",
"Starts": "1",
"Wins": "0",
"Thirds": "0"
},
{
"Name": "Soft"
},
{
"Name": "Heavy"
},
{
"Name": "Synthetic"
},
{
"Name": "Wet"
},
{
"Name": "NonWet",
"PrizeMoney": "0.00",
"Seconds": "0",
"Starts": "1",
"Wins": "0",
"Thirds": "0"
},
{
"Name": "Night"
},
{
"Name": "JumpsRecord"
},
{
"Name": "ThisSeason",
"PrizeMoney": "0.00",
"Seconds": "0",
"Starts": "1",
"Wins": "0",
"Thirds": "0"
}
]
},
"LastStarts": {
"HorseRaceSummary": {
"MeetingCategory": "Professional",
"AbrVenue": "FLEM",
"MoneyBonus": "0.00",
"VenueName": "Flemington",
"MoneyRaceGrandTotal": "121000.00",
"StartingWinPrice": "$13",
"Jockey": {
"JockeyCode": "25791",
"State": "VIC",
"PreferredName": "Vlad",
"Surname": "Duric",
"Location": "Pakenham",
"Postcode": "3810",
"Initials": "V",
"Apprentice": "false",
"Name": "Vlad Duric"
},
"RaceCode": "5038498",
"ClubCategory": "Metro",
"RaceDistance": "1000",
"RaceStarters": "11",
"EntryConditions": {
"EntryCondition": [
{
"Medium": "No class restriction",
"Short": "",
"Long": "No class restriction",
"Code": "9",
"Type": "Class",
"Id": "~"
},
{
"Medium": "Set Weights",
"Short": "SW",
"Long": "Set Weights",
"Code": "50",
"Type": "Weight",
"Id": "SW"
},
{
"Medium": "2-Y-O",
"Short": "2Y",
"Long": "Two-Years-Old",
"Code": "33",
"Type": "Age",
"Id": "2"
},
{
"Medium": "No sex restriction",
"Short": "",
"Long": "No sex restriction",
"Code": "0",
"Type": "Sex",
"Id": "~"
}
]
},
"VenueCode": "151",
"GroupType": "LR",
"InRunning": [
{
"Distance": "800",
"Position": "5"
},
{
"Distance": "400",
"Position": "8"
}
],
"OtherRunners": {
"OtherRunner": [
{
"JockeyCode": "22573",
"HorseCode": "985390",
"Weight": "55.00",
"Barrier": "7",
"JockeyName": "M J Zahra",
"WeightCarried": "55.00",
"Time": "00:00:57.960",
"FinishPosition": "1",
"HorseName": "Mihalic",
"WinningMargin": "8.80"
},
{
"JockeyCode": "793032",
"HorseCode": "983001",
"Weight": "57.00",
"BeatenMargin": "0.75",
"JockeyName": "D M Lane",
"WeightCarried": "57.00",
"Barrier": "9",
"FinishPosition": "2",
"HorseName": "Iceflow"
},
{
"JockeyCode": "488500",
"HorseCode": "989316",
"Weight": "57.00",
"BeatenMargin": "2.25",
"JockeyName": "M J Walker",
"WeightCarried": "57.00",
"Barrier": "8",
"FinishPosition": "3",
"HorseName": "Lake Jackson"
}
]
},
"NameRaceForm": "MARIBYRNONG TRL",
"RaceType": "Flat",
"Handicap": "57.00",
"HandicapRatingPreRace": "0.00",
"TimeSectional": "00:00:33.820",
"BarrierNumber": "5",
"MtgDate": "2014-10-04",
"TrackCondition": {
"NumericTrackRating": "3"
},
"BettingFluctuations": {
"PriceOpen": "$11",
"PriceMove1": "$13"
},
"DistanceSectional": "600",
"ClubCode": "10018",
"RaceNumber": "1",
"MoneyPrize": "0.00",
"NameRaceNews": "Maribyrnong Trial Stakes",
"VenueCategory": "Metro",
"FinishPosition": {
"OfficialMargin": "1/2 Neck",
"BeatenMargin": "8.80"
},
"DayNight": "Day",
"WeightCarried": "57.00"
}
}
},
"Logos": {
"Logo": {
"Image": "VOBIS.png",
"Reason": "Bonus Scheme",
"Id": "VOBIS",
"Description": "Super Vobis Bonus Scheme"
}
},
"HandicapRating": "0.00",
"BallotSequence": "7=",
"TabNumber": "1",
"HandicapWeight": "57.00",
"JockeyRaceEntry": {
"JockeyCode": "25791",
"State": "VIC",
"PreferredName": "Vlad",
"Surname": "Duric",
"Location": "Pakenham",
"Postcode": "3810",
"Initials": "V",
"Apprentice": "false",
"RidingWeight": "53.00",
"Name": "Vlad Duric"
},
"BonusIndicator": {
"SchemeName": "Super Vobis"
},
"RaceEntryCode": "9153413",
"HorseOwnership": {
"OfficialHorseOwners": "Boom Racing 4 (Mgr J L Travaglia)"
},
"RaceEntryStatusEncoded": "67601",
"LastGear": {
"GearType": "Blinkers ON",
"GearDate": "2014-09-29"
},
"Breeding": {
"Breeder": "Burnewang North Pastoral",
"Dam": {
"HorseName": "Apologies",
"HorseCode": "752264"
},
"SireOfDam": {
"Country": "USA",
"HorseName": "Giant's Causeway",
"HorseCode": "604480"
},
"Sire": {
"HorseName": "Kaphero",
"HorseCode": "726892"
}
},
"RacingColours": "Purple, Yellow V, Purple And Yellow Diagonal Striped Sleeves, Yellow And Purple Stars Cap",
"WeightPenalty": "0.0"
}
},
"RaceStartTime": {
"TimeAtVenue": "2014-10-04T12:40:00",
"AsUtc": "2014-10-04T02:40:00"
},
"EntryConditions": {
"EntryCondition": [
{
"Medium": "No class restriction",
"Short": "",
"Long": "No class restriction",
"Code": "9",
"Type": "Class",
"Id": "~"
},
{
"Medium": "Set Weights",
"Short": "SW",
"Long": "Set Weights",
"Code": "50",
"Type": "Weight",
"Id": "SW"
},
{
"Medium": "2-Y-O",
"Short": "2Y",
"Long": "Two-Years-Old",
"Code": "33",
"Type": "Age",
"Id": "2"
},
{
"Medium": "No sex restriction",
"Short": "",
"Long": "No sex restriction",
"Code": "0",
"Type": "Sex",
"Id": "~"
},
{
"Medium": "Early Season 2YO",
"Short": "2YO SW",
"Long": "Selected Early Season 2YO",
"Code": "430001",
"Type": "Ballot",
"Id": "BC7"
}
]
},
"SizeEmergency": "4",
"TrackStraight": "448",
"DistanceApprox": "false",
"ClosingTimes": {
"LateNominations": "2014-09-29T15:00:00",
"WeightsPublishing": "2014-09-30T00:00:00",
"UtcOffset": "10:00:00.000",
"Scratching": "2014-10-04T07:30:00",
"Nominations": "2014-09-29T12:00:00",
"AcceptanceFinal": "2014-10-01T10:00:00",
"Riders": "2014-10-01T13:00:00"
},
"RaceNumber": "1",
"WeightChange": "0.00",
"BallotedOutEntries": null
}
},
"MeetingStageEncoded": "15",
"ApprenticeMeetingType": "Metro",
"MeetCode": "859382",
"DayNight": "Day",
"AcceptanceClose": "2014-10-01T10:00:00+10:00",
"GenerationDate": "2014-12-05T10:09:53+11:00"
}
}
输出可能有点长,但如果你查看输出的底部,所有值都会反转:
...
"MeetingStageEncoded": "15",
"ApprenticeMeetingType": "Metro",
"MeetCode": "859382",
"DayNight": "Day",
"AcceptanceClose": "2014-10-01T10:00:00+10:00",
"GenerationDate": "2014-12-05T10:09:53+11:00"
...
我希望这就是你要找的东西。
答案 1 :(得分:1)
看起来像这样:
myXML = Crack::XML.parse(File.read("20141004Flemington_5start.xml"))
myJSON = myXML.to_json
final = JSON.parse(myJSON,:symbolize_names => true)
pp final
File.write('test2.json', final)
应该是:
myXML = Crack::XML.parse(File.read("20141004Flemington_5start.xml"))
File.write('test2.json', myXML.to_json)
如果你想要JSON输出。