c# - JSON.Net Xml 序列化误解了数组

标签 c# json json.net

我有一些自动生成的 xml,其中 xml 的某些部分可能有多行,而有些可能没有。结果是,如果有一行,则返回单个 json 节点,如果我有多行,则返回带有 json 节点的数组。

xmls 可能看起来像这样

<List>
    <Content>
        <Row Index="0">
            <Title>Testing</Title>
            <PercentComplete>0</PercentComplete>
            <DueDate/>
            <StartDate/>
        </Row>
    </Content>
</List>

或多行

<List>
    <Content>
        <Row Index="0">
            <Title>Update Documentation</Title>
            <PercentComplete>0.5</PercentComplete>
            <DueDate>2013-01-31 00:00:00</DueDate>
            <StartDate>2013-01-01 00:00:00</StartDate>
        </Row>
        <Row Index="1">
            <Title>Write jQuery example</Title>
            <PercentComplete>0.05</PercentComplete>
            <DueDate>2013-06-30 00:00:00</DueDate>
            <StartDate>2013-01-02 00:00:00</StartDate>
        </Row>
    </Content>
</List>

当使用将这些序列化为 JSON 时

JsonConvert.SerializeXmlNode(xmldoc, Formatting.Indented);

第一个xml变成这个

{
    "List": {
        "Content": {
            "Row": {
                "@Index": "0",
                "Title": "Testing",
                "PercentComplete": "0",
                "DueDate": null,
                "StartDate": null
            }
        }
    }
}

第二个

{
    "List": {
        "Content": {
            "Row": [{
                "@Index": "0",
                "Title": "Update Documentation",
                "PercentComplete": "0.5",
                "DueDate": "2013-01-31 00:00:00",
                "StartDate": "2013-01-01 00:00:00"
            }, {
                "@Index": "1",
                "Title": "Write jQuery example",
                "PercentComplete": "0.05",
                "DueDate": "2013-06-30 00:00:00",
                "StartDate": "2013-01-02 00:00:00"
            }]
        }
    }
}

可以清楚地看到第二个的 Row 是一个数组,但不是第一个。对于此类问题是否有任何已知的解决方法,或者我是否需要在接收 JSON 的前端中实现检查(这会有点问题,因为结构非常动态)。最好的方法是如果有任何方法可以强制 json.net 始终返回数组。

最佳答案

来自 Json.NET 文档: http://james.newtonking.com/projects/json/help/?topic=html/ConvertingJSONandXML.htm

您可以通过将属性 json:Array='true' 添加到要转换为 JSON 的 XML 节点来强制将节点呈现为数组。此外,您需要在 XML header xmlns:json='http://james.newtonking.com/projects/json' 处声明 json 前缀命名空间,否则您将收到一个 XML 错误说明json前缀没有声明。

下一个示例由文档提供:

xml = @"<person xmlns:json='http://james.newtonking.com/projects/json' id='1'>
        <name>Alan</name>
        <url>http://www.google.com</url>
        <role json:Array='true'>Admin</role>
      </person>";

生成的输出:

{
  "person": {
    "@id": "1",
    "name": "Alan",
    "url": "http://www.google.com",
    "role": [
      "Admin"
    ]
  }
}

关于c# - JSON.Net Xml 序列化误解了数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14488662/

相关文章:

c# - Windows Form C#的本地化

c# - 让 Graphic 最初显示出来

c# - 处理 JObject 与 JArray

javascript - jquery ajax 内部循环返回 statusText 错误,在 IE 中一定时间后状态为 0

javascript - 将字符串转换为 JSON

c# - 尝试将浮点解析为 int 时,Web API 2 中的 Newtonsoft JSON 反序列化无提示地失败

c# - 使用 C# 在一行中读取两个整数

java - 改造响应方法从谷歌地图 API 解析 JSON 不起作用

c# - 如何反序列化包含 JSON.NET 中的字符串和对象的数组

c# - 强制 JSON.NET 在序列化 DateTime 时包含毫秒(即使 ms 组件为零)