c# - JSON Deserialise 处理 Array 但不喜欢单个“对象”
问题描述
我正在处理传入的 XML 数据,数据的“顶部”是固定的,其中的“行”可能在内容和数量上有所不同。
我的代码在反序列化为 JSON 然后到数据表时有效,但如果行数为 1,则失败。我收到此错误消息:
Newtonsoft.Json.JsonSerializationException:无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型“DynamicJSON.DynamicJSON+Row[]”,因为该类型需要 JSON 数组(例如 [1,2,3 ]) 正确反序列化。
我可能错过了一些非常明显的东西,但还没有找到答案。
我已经尝试过这篇文章中的解决方案,但最终出现堆栈溢出错误(有点讽刺......) 处理 JSON 单个对象和数组
我处理原始 xml 的代码:
XmlDocument doc = new XmlDocument();
doc.LoadXml(File.ReadAllText("C:\\temp\\file.xml").ToString());
string json = JsonConvert.SerializeXmlNode(doc);
DynamicJSON.Rootobject r = JsonConvert.DeserializeObject<DynamicJSON.Rootobject>(json);
JObject jObject = JObject.Parse(json);
JArray jArray = (JArray)jObject["SOAP-ENV:Envelope"]["SOAP-ENV:Body"]["reportresponse"]["row"];
DataTable jdt = (DataTable)JsonConvert.DeserializeObject(jArray.ToString(), (typeof(DataTable)));
我的课
class DynamicJSON
{
public class Rootobject
{
[JsonProperty("SOAP-ENV:Envelope")]
public SOAPENVEnvelope SOAPENVEnvelope { get; set; }
}
public class SOAPENVEnvelope
{
public string xmlnsSOAPENV { get; set; }
public string xmlnsSOAPENC { get; set; }
public string xmlnsxsi { get; set; }
public string xmlnsxsd { get; set; }
[JsonProperty("SOAP-ENV:Header")]
public SOAPENVHeader SOAPENVHeader { get; set; }
[JsonProperty("SOAP-ENV:Body")]
public SOAPENVBody SOAPENVBody { get; set; }
}
public class SOAPENVHeader
{
public string reportname { get; set; }
public string reportstartdate { get; set; }
public string reportenddate { get; set; }
public string reportmerchantid { get; set; }
}
public class SOAPENVBody
{
public Reportresponse reportresponse { get; set; }
[JsonProperty("SOAP-ENV:Fault")]
public SOAPENVFault? SOAPENVFault { get; set; }
}
public class SOAPENVFault
{
public string faultcode { get; set; }
public string faultstring { get; set; }
}
[JsonConverter(typeof(SingleOrArrayConverter<Row>))]
public class Reportresponse
{
public Row[] row { get; set; }
}
public class Row
{
[JsonExtensionData]
public Dictionary<string, JToken> child { get; set; }
public Row()
{
child = new Dictionary<string, JToken>();
}
}
}
我的 XML 如下所示:
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Header>
<reportname>ReportName</reportname>
<reportstartdate>2020-Jun-1</reportstartdate>
<reportenddate>2020-Jun-1</reportenddate>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<reportresponse>
<row>
<rowid>1</rowid>
<value1>1</value1>
<value2>1</value2>
<value3>1</value3>
</row>
</reportresponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
JSON 被转换为此(作为单个“行”)
{
"SOAP-ENV:Envelope": {
"@xmlns:SOAP-ENV": "http://schemas.xmlsoap.org/soap/envelope/",
"@xmlns:SOAP-ENC": "http://schemas.xmlsoap.org/soap/encoding/",
"@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"@xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"SOAP-ENV:Header": {
"reportname": "ReportName",
"reportstartdate": "2020-Jun-1",
"reportenddate": "2020-Jun-1"
},
"SOAP-ENV:Body": {
"reportresponse": {
"row": {
"rowid": "1",
"value1": "1",
"value2": "1",
"value3": "1"
}
}
}
}
}
JSON数组:
{
"SOAP-ENV:Envelope": {
"@xmlns:SOAP-ENV": "http://schemas.xmlsoap.org/soap/envelope/",
"@xmlns:SOAP-ENC": "http://schemas.xmlsoap.org/soap/encoding/",
"@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"@xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"SOAP-ENV:Header": {
"reportname": "ReportName",
"reportstartdate": "2020-Jun-1",
"reportenddate": "2020-Jun-1"
},
"SOAP-ENV:Body": {
"reportresponse": {
"row": [
{
"rowid": "1",
"value1": "1",
"value2": "1",
"value3": "1"
},
{
"rowid": "2",
"value1": "2",
"value2": "2",
"value3": "2"
}
]
}
}
}
}
我尝试过不使用 SingleOrArrayConverter,我也尝试过使用动态和字符串来代替,SingleOrArrayConverter<Row>
但到目前为止还没有运气。
感谢任何人(礼貌地)告诉我我在哪里错过了明显的..
解决方案
推荐阅读
- html - 如何以角度刷新扩展面板?
- git - git 正在重新创建具有不同索引的文件夹
- sql - 避免 SQL Server SELECT 语句中的计算列代码重复
- c++ - 编译器报错库函数排序的第三个参数
- python - Pandas - 创建数据框
- php - 导航到该子类别后禁用子类别文本的呈现 - woocommerce
- r - 如何查看以其中一个值为条件的几列?
- internationalization - hreflang 属性在 ` ` 和 ` `中是否遵循不同的语言代码规则?
- android - 只需一次创建和填充数据 UI - Jetpack Compose 和 MVVM 架构
- python - 数数 从一个数据框列表到另一个数据框列表的匹配词