我使用This XMLToJson转换脚本将XML转换为JSON。
这样可以正常工作,但是剩下的JSON包含一些元素值的#和@。
{"#text":"Sat, 16 May 2015 09:48:23 +1200"}
我使用$ .each循环来遍历新创建的JSON文件中的所有项目。当我尝试下面的日期时。我收到一个非法的char错误,即使用\来取消它。
> `console.log(entry.pubDate.#text));`
我可以使用JSON.stringify,然后将我不喜欢的部分子串出来,但这很麻烦,并且最开始将它转换为JSON。
将此pubDate.#text
值设为字符串的最佳方法是什么?
JSON树看起来像这样:
{
"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"
}
}
}
}
答案 0 :(得分:1)
您还可以使用括号引用JSON。所以,同时:
console.log(entry.pubDate.#text);
由于SyntaxError: Unexpected token ILLEGAL
,可能会失败,您可以这样做:
console.log(entry.pubDate["#text"]);
我建议只使用这种方式连接JSON,而不是解析#
和@
个字符。
所以,假设我有一个非常奇怪的对象,名为a
,看起来像:
{
"#data":{
"@values":[
1,
2,
3
]
}
}
我可以通过调用values
a["#data"]["@values"][0]
中的第一个整数
希望这能帮到你!