Reputation: 1883
I am using This XMLToJson convertion script to convert XML to JSON.
This works fine, however the JSON left over contains # and @ for some of the element values.
{"#text":"Sat, 16 May 2015 09:48:23 +1200"}
I am using an $.each loop to iterate through all the items in the newly created JSON file. When I attempt the date below. I get an illegal char error, even with a \ to cancel it out.
> `console.log(entry.pubDate.#text));`
I could use JSON.stringify and then substring out the parts I don't wont, but that is messy and betrays the point of converting it to JSON in the first place.
What is the best way to get this pubDate.#text
value to string?
The JSON Tree looks like this:
{
"rss":{
"@attributes":{
"xmlns:dc":"http://purl.org/dc/elements/1.1/",
"version":"2.0"
},
"channel":{
"title":{
"#text":"Stuff.co.nz - Stuff"
},
"link":{
"#text":"http://www.stuff.co.nz"
},
"description":{
"#text":"Top Stories from Stuff.co.nz. New Zealand, world, sport, business & entertainment news on Stuff.co.nz."
},
"language":{
"#text":"en-nz"
},
"copyright":{
"#text":"Fairfax New Zealand Ltd."
},
"dc:language":{
"#text":"en-nz"
},
"dc:rights":{
"#text":"Fairfax New Zealand Ltd."
},
"item":[
{
"title":{
"#text":"Wintry winds as heavy rains move on"
},
"link":{
"#text":"http://www.stuff.co.nz/national/68604072/Wintry-winds-as-heavy-rains-move-on"
},
"description":{
"#text":"The heavy rains that brought flooding to the lower North Island have mostly moved on now, but a cold southwesterly keeps things feeling wintry."
},
"enclosure":{
"@attributes":{
"url":"http://static2.stuff.co.nz/1431728712/600/11778600.jpg",
"length":"2811",
"type":""
}
},
"author":{
},
"guid":{
"@attributes":{
"isPermaLink":"false"
},
"#text":"stuff.co.nz/68604072"
},
"dc:creator":{
},
"dc:date":{
"#text":"Sat, 16 May 2015 09:48:23 +1200"
},
"pubDate":{
"#text":"Sat, 16 May 2015 09:48:23 +1200"
}
},
{
"title":{
"#text":"Couple celebrates ill baby's birthday in unique way"
},
"link":{
"#text":"http://www.stuff.co.nz/life-style/well-good/inspire-me/68601237/Couple-celebrates-ill-babys-birthday-in-unique-way"
},
"description":{
"#text":"Baby Jai Bishop has lived at Starship Hospital for the past seven months, with his parents flying back and forth from Hokitika to be by his side."
},
"enclosure":{
"@attributes":{
"url":"http://static2.stuff.co.nz/1431727477/539/11778539.jpg",
"length":"3575",
"type":""
}
},
"author":{
"#text":"Aileen Nakhle"
},
"guid":{
"@attributes":{
"isPermaLink":"false"
},
"#text":"stuff.co.nz/68601237"
},
"dc:creator":{
"#text":"Aileen Nakhle"
},
"dc:date":{
"#text":"Sat, 16 May 2015 10:03:57 +1200"
},
"pubDate":{
"#text":"Sat, 16 May 2015 10:03:57 +1200"
}
},
{
"title":{
"#text":"Sikh man removes turban to help child"
},
"link":{
"#text":"http://www.stuff.co.nz/national/68604241/Sikh-man-removes-turban-to-help-child"
},
"description":{
"#text":"A young Sikh man has been praised for breaking religious protocol and using his turban to cradle the head of an injured child in Auckland."
},
"enclosure":{
"@attributes":{
"url":"http://static2.stuff.co.nz/1431722799/399/11778399.jpg",
"length":"4017",
"type":""
}
},
"author":{
},
"guid":{
"@attributes":{
"isPermaLink":"false"
},
"#text":"stuff.co.nz/68604241"
},
"dc:creator":{
},
"dc:date":{
"#text":"Sat, 16 May 2015 08:46:26 +1200"
},
"pubDate":{
"#text":"Sat, 16 May 2015 08:46:26 +1200"
}
},
{
"title":{
"#text":"White kiwi chicks killed at Pukaha Mt Bruce"
},
"link":{
"#text":"http://www.stuff.co.nz/environment/68574558/White-kiwi-chicks-killed-at-Pukaha-Mt-Bruce"
},
"description":{
"#text":"Two rare white kiwi chicks are among seven kiwis killed by an \"evil\" predator at Pukaha Mt Bruce National Wildlife Centre."
},
"enclosure":{
"@attributes":{
"url":"http://static2.stuff.co.nz/1431727078/519/11778519.jpg",
"length":"3785",
"type":""
}
},
"author":{
"#text":"Caleb Harris"
},
"guid":{
"@attributes":{
"isPermaLink":"false"
},
"#text":"stuff.co.nz/68574558"
},
"dc:creator":{
"#text":"Caleb Harris"
},
"dc:date":{
"#text":"Fri, 15 May 2015 23:17:16 +1200"
},
"pubDate":{
"#text":"Fri, 15 May 2015 23:17:16 +1200"
}
},
{
"title":{
"#text":"LIVE: Prince Harry in New Zealand - Day 8"
},
"link":{
"#text":"http://www.stuff.co.nz/auckland/local-news/68594486/LIVE-Prince-Harry-in-New-Zealand-Day-8"
},
"description":{
"#text":"Prince Harry is spending the morning at the Cloud in Auckland, where he's set to take on schoolkids in a quick football match. "
},
"enclosure":{
"@attributes":{
"url":"http://static2.stuff.co.nz/1431729920/638/11778638.jpg",
"length":"3975",
"type":""
}
},
"author":{
},
"guid":{
"@attributes":{
"isPermaLink":"false"
},
"#text":"stuff.co.nz/68594486"
},
"dc:creator":{
},
"dc:date":{
"#text":"Sat, 16 May 2015 10:44:54 +1200"
},
"pubDate":{
"#text":"Sat, 16 May 2015 10:44:54 +1200"
}
},
{
"title":{
"#text":"Man pleads guilty to killing stepfather with 'atomic wedgie'"
},
"link":{
"#text":"http://www.stuff.co.nz/world/americas/68604525/Man-pleads-guilty-to-killing-stepfather-with-atomic-wedgie"
},
"description":{
"#text":"An American man has pleaded guilty to manslaughter after he killed his stepfather with an \"atomic wedgie\"."
},
"enclosure":{
"@attributes":{
"url":"http://static2.stuff.co.nz/1431727773/570/11778570.jpg",
"length":"2635",
"type":""
}
},
"author":{
},
"guid":{
"@attributes":{
"isPermaLink":"false"
},
"#text":"stuff.co.nz/68604525"
},
"dc:creator":{
},
"dc:date":{
"#text":"Sat, 16 May 2015 10:06:21 +1200"
},
"pubDate":{
"#text":"Sat, 16 May 2015 10:06:21 +1200"
}
},
{
"title":{
"#text":"Worker injured in Auckland power outage"
},
"link":{
"#text":"http://www.stuff.co.nz/auckland/68604712/Worker-injured-in-Auckland-power-outage"
},
"description":{
"#text":"A worker has been injured in an incident which resulted in power being cut to up to 10,000 homes on Auckland's North Shore this morning, lines company Vector says."
},
"author":{
"#text":"Henry Cooke"
},
"guid":{
"@attributes":{
"isPermaLink":"false"
},
"#text":"stuff.co.nz/68604712"
},
"dc:creator":{
"#text":"Henry Cooke"
},
"dc:date":{
"#text":"Sat, 16 May 2015 11:01:48 +1200"
},
"pubDate":{
"#text":"Sat, 16 May 2015 11:01:48 +1200"
}
},
{
"title":{
"#text":"U2's The Edge falls off stage"
},
"link":{
"#text":"http://www.stuff.co.nz/entertainment/celebrities/68604341/U2s-The-Edge-falls-off-stage"
},
"description":{
"#text":"U2 guitarist The Edge says he's fine after falling off the stage at a concert in Canada."
},
"enclosure":{
"@attributes":{
"url":"http://static2.stuff.co.nz/1431728971/605/11778605.jpg",
"length":"2611",
"type":""
}
},
"author":{
},
"guid":{
"@attributes":{
"isPermaLink":"false"
},
"#text":"stuff.co.nz/68604341"
},
"dc:creator":{
},
"dc:date":{
"#text":"Sat, 16 May 2015 10:29:09 +1200"
},
"pubDate":{
"#text":"Sat, 16 May 2015 10:29:09 +1200"
}
}
],
"ttl":{
"#text":"30"
}
}
}
}
Upvotes: 0
Views: 107
Reputation: 6258
You can also reference JSON using brackets. So while:
console.log(entry.pubDate.#text);
might fail due to SyntaxError: Unexpected token ILLEGAL
, you could just do this:
console.log(entry.pubDate["#text"]);
I would recommend just interfacing the JSON in this way, rather than parsing the #
and @
characters out.
So let's say I have a really weird object, called a
that looks like:
{
"#data":{
"@values":[
1,
2,
3
]
}
}
I can get the first integer from values
from it by calling: a["#data"]["@values"][0]
Hope this helps you!
Upvotes: 1