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 序列化误解了数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)