当前的JSON.NET构建不提供请求的功能,因此我修改了源代码以提供此功能:
https://github.com/lukegothic/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs
此修改为XmlNodeConverter提供了一种从XML节点(称为“ Type”)读取可选属性的方法,该属性保存所需的节点值序列化。默认情况下,转换器将所有值序列化为结果JSON字符串中的字符串,但是现在您可以添加一个属性,该属性指定所需的DataType输出。允许的类型为Integer,Float,Boolean和Date。
例如,如果您具有以下源XML:
<Object>
<ID json:Type='Integer'>12</ID>
<Title>mytitle</Title>
<Visible json:Type='Boolean'>false</Visible>
<Price json:Type='Float'>1.55</Price>
<ExpirationDate json:Type='Date'>2013-12-31</ExpirationDate>
</Object>
它将被序列化为:
{
"ID":12,
"Title":"mytitle",
"Visible":false,
"Price":1.55,
"ExpirationDate":"2013-12-31T00:00:00"
}