【问题标题】:Parse XML Converted to JSON with # and @使用 # 和 @ 解析 XML 转换为 JSON
【发布时间】:2015-07-28 00:04:01
【问题描述】:

我正在使用This XMLToJson 转换脚本将 XML 转换为 JSON。

这很好用,但是剩下的 JSON 包含一些元素值的 # 和 @。

{"#text":"Sat, 16 May 2015 09:48:23 +1200"}

我正在使用 $.each 循环遍历新创建的 JSON 文件中的所有项目。当我尝试下面的日期时。我得到一个非法字符错误,即使用 \ 来取消它。

> `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"
         }
      }
   }
}

【问题讨论】:

标签: jquery ajax json xml loops


【解决方案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]

希望对您有所帮助!

【讨论】:

  • 就语义而言,不,您不能“使用方括号引用 JSON。”您可以将 JSON 解析(反序列化)为 JavaScript,其中可以通过方括号表示法访问对象属性。记住,JSON 是一个字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-20
相关资源
最近更新 更多