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

JSON.Net Xml Serialization misunderstands arrays(JSON.Net Xml 序列化误解了数组)

本文介绍了JSON.Net Xml 序列化误解了数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some autogenerated xmls where some parts of the xml may have multiple rows and some may not. The result is that if there is one row a single json node is returned and if I have multiple rows an array with json nodes are returned.

xmls 可能看起来像这样

The xmls may look like this

<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 时

When serializing these to JSON using

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 始终返回数组.

As clearly can be seen the Row on the second one is an array as should be but not on the first one. Is there any known workaround on this kind of issues or do I need to implement the check in my frontend receiving the JSON (that would be a bit problematic since the structures are very dynamic). The best way would be if there where any way to enforce json.net to always return arrays.

推荐答案

我确实像这样修复了这种行为

I did fix this behavior like this

// Handle JsonConvert array bug
var rows = doc.SelectNodes("//Row");
if(rows.Count == 1)
{
    var contentNode = doc.SelectSingleNode("//List/Content");
    contentNode.AppendChild(doc.CreateNode("element", "Row", ""));

    // Convert to JSON and replace the empty element we created but keep the array declaration
    returnJson = JsonConvert.SerializeXmlNode(doc).Replace(",null]", "]");
}
else
{
    // Convert to JSON
    returnJson = JsonConvert.SerializeXmlNode(doc);
}

它有点脏,但它有效.我仍然对其他解决方案感兴趣!

It's a bit dirty but it works. I'm still interested in other solutions!

这篇关于JSON.Net Xml 序列化误解了数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:JSON.Net Xml 序列化误解了数组

基础教程推荐