从XML迁移到JSON,我正在转换它吗?

时间:2014-03-07 10:35:46

标签: xml json language-agnostic

以前,我使用XML来存储有关我的应用用于处理的播客的信息。但是,使用解析器的复杂性使我从XML转换为JSON以及其他一些原因。

以下是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<show-list count="23">
    <show>
        <title>TQA Weekly</title>
        <description> 
            <![CDATA[
            Technology Show, dedicated to those who wish to learn about new electronics that they have bought, or will buy soon. 
            We will explaining in each episode new ways of doing things like protecting your identity online, file backup and storage, encryption, using email wisely, and each show we will be giving you new tools to do so. 
            You may visit our web-site for show notes, lists of software, links to sites, other suggested web-sites, or to send e-mails to Steve Smith with questions, comments or concerns.
            ]]>
        </description>
        <host>Steve Smith</host>
        <logo>http://images.tqaweekly.com/tqa-weekly-logo.png</logo>
        <feed>http://feeds.podtrac.com/tTKj5t05olM$</feed>
    </show>
<!-- more shows -->  

这是我的JSON的样子:

{
    "count" : 23;
    [
        "show" : {
                    "title" : "TQA Weekly",
                    "description" : "Technology show",
                    "host" : "Steve Smith",
                    "logo" : "http://images.tqaweekly.com/tqa-weekly-logo.png",
                    "feed" : "http://feeds.podtrac.com/tTKj5t05olM$"
                 },

        "show" : {
                    "title" : "TWiT",
                    "description" : "This Week In Tech",
                    "host" : "Sarah Lane, etc",
                    "logo" : "http://logo/url",
                    "feed" : "http://feed/url"
                 }

        // more shows
    ]
}  

我的问题是:我是否正确完成了从XML到JSON的转换?目的是在JSON中表示相同的信息。

我确信我和其他人一起犯了语法错误。请指出它们。我是JSON的新手

1 个答案:

答案 0 :(得分:1)

嗯,这不是有效的JSON(正如你在尝试解析它时所看到的那样),所以没有。

例如:

  • "count" : 23之后的分号错误(应该是逗号),
  • 之后有一个列表,它不应该是(之前应该有一个标识符)
  • 该列表包含字典之类的元素,因此这些是非法的
  • 如果该列表是字典,那么它将是非法的,因为它包含两次相同的密钥。

有效的陈述是

{
    "count" : 23,
    "shows": [
        {
                    "title" : "TQA Weekly",
                    "description" : "Technology show",
                    "host" : "Steve Smith",
                    "logo" : "http://images.tqaweekly.com/tqa-weekly-logo.png",
                    "feed" : "http://feeds.podtrac.com/tTKj5t05olM$"
        },
        {
                    "title" : "TWiT",
                    "description" : "This Week In Tech",
                    "host" : "Sarah Lane, etc",
                    "logo" : "http://logo/url",
                    "feed" : "http://feed/url"
        }
    ]
}