JSON.Net XML序列化误解阵列 [英] JSON.Net Xml Serialization misunderstands arrays

查看:172
本文介绍了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.

该XML的可能看起来像这样

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成为本

The first xml becomes this

{
    "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"
            }]
        }
    }
}

正如显然可以看出行上,第二个是作为应,但是不能在第一个阵列。是否有这样的问题,任何已知的解决方法或做我需要实现我的前端接收的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序列化误解阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆