c# - 当我将 JSON 对象传递给它时,C# JSON 反序列化尝试反序列化为 KeyValuePairs 数组
问题描述
我有一个错误:
无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“System.Collections.Generic.Dictionary`2[System.String,Tessa.Extensions.Shared.Acquiring.AcqFieldData]”,因为该类型需要 JSON对象(例如 {"name":"value"})正确反序列化。
当我将 JSON 数组字符串(设置)传递给JsonConvert.DeserializeObject<Dictionary<String, AcqFieldData>>(settings)
和另一个错误:
无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.KeyValuePair`2[System.String,Tessa.Extensions.Shared.Acquiring.AcqFieldRef][]',因为type 需要一个 JSON 数组(例如 [1,2,3])才能正确反序列化。
当我将 JSON 对象字符串作为输入传递时。这是我的两个输入,数组:
[
{
"eca31ead-e905-43af-b92d-d5c01eccb590": {
"Fields": {
"EquipmentManufacturerName": {
"CardID": "3ae9344f-00f7-4760-a790-4b5375807791",
"Section": "AcqEquipmentManufacturerCards",
"Field": "Name",
"Value": "ref"
},
"EquipmentManufacturerID": {
"CardID": null,
"Section": null,
"Field": null,
"Value": "3ae9344f-00f7-4760-a790-4b5375807791"
},
"IsClientEquipment": {
"CardID": null,
"Section": null,
"Field": null,
"Value": false
},
"ServiceCatID": {
"CardID": null,
"Section": null,
"Field": null,
"Value": null
},
"ServiceCatName": {
"CardID": null,
"Section": null,
"Field": null,
"Value": null
}
},
"ReadonlyControlAliases": null
}
},
{
"67830499-876a-4e2c-a08d-fd6100349b99": {
"Fields": {
"ClassBName": {
"CardID": "0fb18fa1-8560-4364-9a16-03815f5e9303",
"Section": "AcqClassBCards",
"Field": "Name",
"Value": "ref"
},
"ClassBID": {
"CardID": null,
"Section": null,
"Field": null,
"Value": "0fb18fa1-8560-4364-9a16-03815f5e9303"
},
"TerminalTypeName": {
"CardID": "7c081fbf-13e6-4949-a634-85d7a99ebe01",
"Section": "AcqTerminalTypeCards",
"Field": "Name",
"Value": "ref"
},
"TerminalTypeID": {
"CardID": null,
"Section": null,
"Field": null,
"Value": "7c081fbf-13e6-4949-a634-85d7a99ebe01"
},
"EquipmentManufacturerName": {
"CardID": "3ae9344f-00f7-4760-a790-4b5375807791",
"Section": "AcqEquipmentManufacturerCards",
"Field": "Name",
"Value": "ref"
},
"EquipmentManufacturerID": {
"CardID": null,
"Section": null,
"Field": null,
"Value": "3ae9344f-00f7-4760-a790-4b5375807791"
},
"FloorLimitSum": {
"CardID": null,
"Section": null,
"Field": null,
"Value": null
},
"FloorLimit": {
"CardID": null,
"Section": null,
"Field": null,
"Value": false
},
"CreditVaucherSum": {
"CardID": null,
"Section": null,
"Field": null,
"Value": null
},
"CreditVaucher": {
"CardID": null,
"Section": null,
"Field": null,
"Value": false
},
"MotoSof": {
"CardID": null,
"Section": null,
"Field": null,
"Value": false
},
"Preauthorization": {
"CardID": null,
"Section": null,
"Field": null,
"Value": false
},
"IsClientEquipment": {
"CardID": null,
"Section": null,
"Field": null,
"Value": true
},
"ServiceCatID": {
"CardID": null,
"Section": null,
"Field": null,
"Value": null
},
"ServiceCatName": {
"CardID": null,
"Section": null,
"Field": null,
"Value": null
}
},
"ReadonlyControlAliases": [
"ClassB",
"TerminalType",
"EquipmentManufacturer",
"FloorLimitSum",
"FloorLimit",
"CreditVaucherSum",
"CreditVaucher",
"MotoSof",
"Preauthorization",
"IsClientEquipment"
]
}
},
{
"9560cc34-822e-430a-9a42-0eccb6d052f2": {
"Fields": {
"EquipmentTemplate": {
"CardID": null,
"Section": null,
"Field": null,
"Value": "GPRS"
},
"TerminalTypeName": {
"CardID": "7c081fbf-13e6-4949-a634-85d7a99ebe01",
"Section": "AcqTerminalTypeCards",
"Field": "Name",
"Value": "ref"
},
"TerminalTypeID": {
"CardID": null,
"Section": null,
"Field": null,
"Value": "7c081fbf-13e6-4949-a634-85d7a99ebe01"
},
"IsClientEquipment": {
"CardID": null,
"Section": null,
"Field": null,
"Value": false
},
"ServiceCatID": {
"CardID": null,
"Section": null,
"Field": null,
"Value": null
},
"ServiceCatName": {
"CardID": null,
"Section": null,
"Field": null,
"Value": null
}
},
"ReadonlyControlAliases": [
"EquipmentTemplate",
"TerminalType",
"IsClientEquipment"
]
}
}
]
和对象:
{
"eca31ead-e905-43af-b92d-d5c01eccb590": {
"Fields": {
"EquipmentManufacturerName": {
"CardID": "3ae9344f-00f7-4760-a790-4b5375807791",
"Section": "AcqEquipmentManufacturerCards",
"Field": "Name",
"Value": "ref"
},
"EquipmentManufacturerID": {
"CardID": null,
"Section": null,
"Field": null,
"Value": "3ae9344f-00f7-4760-a790-4b5375807791"
},
"IsClientEquipment": {
"CardID": null,
"Section": null,
"Field": null,
"Value": false
},
"ServiceCatID": {
"CardID": null,
"Section": null,
"Field": null,
"Value": null
},
"ServiceCatName": {
"CardID": null,
"Section": null,
"Field": null,
"Value": null
}
},
"ReadonlyControlAliases": null
},
"67830499-876a-4e2c-a08d-fd6100349b99": {
"Fields": {
"ClassBName": {
"CardID": "0fb18fa1-8560-4364-9a16-03815f5e9303",
"Section": "AcqClassBCards",
"Field": "Name",
"Value": "ref"
},
"ClassBID": {
"CardID": null,
"Section": null,
"Field": null,
"Value": "0fb18fa1-8560-4364-9a16-03815f5e9303"
},
"TerminalTypeName": {
"CardID": "7c081fbf-13e6-4949-a634-85d7a99ebe01",
"Section": "AcqTerminalTypeCards",
"Field": "Name",
"Value": "ref"
},
"TerminalTypeID": {
"CardID": null,
"Section": null,
"Field": null,
"Value": "7c081fbf-13e6-4949-a634-85d7a99ebe01"
},
"EquipmentManufacturerName": {
"CardID": "3ae9344f-00f7-4760-a790-4b5375807791",
"Section": "AcqEquipmentManufacturerCards",
"Field": "Name",
"Value": "ref"
},
"EquipmentManufacturerID": {
"CardID": null,
"Section": null,
"Field": null,
"Value": "3ae9344f-00f7-4760-a790-4b5375807791"
},
"FloorLimitSum": {
"CardID": null,
"Section": null,
"Field": null,
"Value": null
},
"FloorLimit": {
"CardID": null,
"Section": null,
"Field": null,
"Value": false
},
"CreditVaucherSum": {
"CardID": null,
"Section": null,
"Field": null,
"Value": null
},
"CreditVaucher": {
"CardID": null,
"Section": null,
"Field": null,
"Value": false
},
"MotoSof": {
"CardID": null,
"Section": null,
"Field": null,
"Value": false
},
"Preauthorization": {
"CardID": null,
"Section": null,
"Field": null,
"Value": false
},
"IsClientEquipment": {
"CardID": null,
"Section": null,
"Field": null,
"Value": true
},
"ServiceCatID": {
"CardID": null,
"Section": null,
"Field": null,
"Value": null
},
"ServiceCatName": {
"CardID": null,
"Section": null,
"Field": null,
"Value": null
}
},
"ReadonlyControlAliases": [
"ClassB",
"TerminalType",
"EquipmentManufacturer",
"FloorLimitSum",
"FloorLimit",
"CreditVaucherSum",
"CreditVaucher",
"MotoSof",
"Preauthorization",
"IsClientEquipment"
]
},
"9560cc34-822e-430a-9a42-0eccb6d052f2": {
"Fields": {
"EquipmentTemplate": {
"CardID": null,
"Section": null,
"Field": null,
"Value": "GPRS"
},
"TerminalTypeName": {
"CardID": "7c081fbf-13e6-4949-a634-85d7a99ebe01",
"Section": "AcqTerminalTypeCards",
"Field": "Name",
"Value": "ref"
},
"TerminalTypeID": {
"CardID": null,
"Section": null,
"Field": null,
"Value": "7c081fbf-13e6-4949-a634-85d7a99ebe01"
},
"IsClientEquipment": {
"CardID": null,
"Section": null,
"Field": null,
"Value": false
},
"ServiceCatID": {
"CardID": null,
"Section": null,
"Field": null,
"Value": null
},
"ServiceCatName": {
"CardID": null,
"Section": null,
"Field": null,
"Value": null
}
},
"ReadonlyControlAliases": [
"EquipmentTemplate",
"TerminalType",
"IsClientEquipment"
]
}
}
这是我的课。AcqFieldData:
public class AcqFieldData
{
public readonly IDictionary<string, AcqFieldRef> Fields;
public readonly IEnumerable<string> ReadonlyControlAliases;
public AcqFieldData(
IEnumerable<string> readonlyControlAliases,
params KeyValuePair<string, AcqFieldRef>[] fields)
{
this.ReadonlyControlAliases = readonlyControlAliases;
this.Fields = new Dictionary<string, AcqFieldRef>();
foreach (var pair in fields)
{
if (pair.Key != null)
this.Fields.Add(pair);
}
}
}
和 AcqFieldRef:
public class AcqFieldRef
{
public readonly Guid? CardID;
public readonly string Section;
public readonly string Field;
public readonly Object Value;
public AcqFieldRef(Guid cardID, string section, string field)
{
this.CardID = cardID;
this.Section = section;
this.Field = field;
this.Value = "ref";
}
public AcqFieldRef(Object value)
{
if (value as String == "ref")
{
throw new ArgumentException(
"Value \"ref\" is reserved for a link description");
}
this.Value = value;
}
[UsedImplicitly]
public AcqFieldRef()
{
}
}
解决方案
您拥有的第一个 json 不是 aDictionary<string, object>
而是List<Dictionary<string, object>>
.
我用动态来回答你的问题,因为在你的问题中没有类的定义AcqFieldData
。
var value = JsonConvert.DeserializeObject<List<Dictionary<string, dynamic>>>(json);
你的第二个 json 是Dictionary<string, object>
.
var value = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(json);
此代码适用于动态对象,但不适用于AcqFieldData
. 您的构造函数在尝试创建/添加对象并将它们插入到您的字典时似乎有一些问题。foreach (var pair in fields)
当我测试您的代码时,它会在字段为空时引发错误。
我建议尝试使用 getter 和 setter 的课程,然后从那里开始。
public class AcqFieldData
{
public Dictionary<string, AcqFieldRef> Fields { get; set; }
public List<string> ReadonlyControlAliases { get; set; }
}
public class AcqFieldRef
{
public Guid? CardID { get; set; }
public string Section { get; set; }
public string Field { get; set; }
public Object Value { get; set; }
}
// Use this in the main for 1st json
var jObject = JsonConvert.DeserializeObject<List<Dictionary<string, AcqFieldData>>>(json);
// Use this in the main for 2nd json
var jObject = JsonConvert.DeserializeObject<Dictionary<string, AcqFieldData>>(json);
推荐阅读
- javascript - Javascript - 将用户输入值与随机生成的值进行比较
- android - Firebase消息,后台的Android应用程序在单击消息后不会拦截推送通知
- android - 如何在 Android Oreo 中使用前台服务捕获图像
- oracle - 如何将base64编码的img src属性插入Oracle中的表中,然后在Oracle Apex的页面上显示
- android - Firebase 使用电子邮件和电话进行验证
- html - 如何在没有垂直滚动条的情况下使图像适合“首屏”
- c# - WPF-combobox selection changed 显示最后选择的项目而不是当前项目
- docker - 运行 minikube start 时找不到 vswitch 的 hyperV
- scala - scala中的项目依赖项和日志问题
- c# - 代码必须先做一件事,然后等待几秒钟,然后继续