[C#JSON反序列化,当我向其传递JSON对象时,试图反序列化为KeyValuePairs数组]]

问题描述 投票:0回答:1
我有一个错误:

无法将当前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] []'因为该类型需要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数组(例如[1,2,3])反序列化为类型'System.Collections.Generic.Dictionary`2 [System.String,Tessa.Extensions.Shared.Acquiring.AcqFieldData ]'...
c# arrays json
1个回答
0
投票
您拥有的第一个json不是Dictionary<string, object>,而是List<Dictionary<string, object>>

我使用动态回答您的问题,因为您的问题中没有定义类AcqFieldData

© www.soinside.com 2019 - 2024. All rights reserved.