![](/img/trans.png)
[英]Using JavaScriptSerializer to Deserialize JSON C# - type not supported for deserialization of an array
[英]C# JSON deserialization trying to deserialize into an array of KeyValuePairs when i pass a JSON object to it
我有一個錯誤:
無法將當前 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 不是Dictionary<string, object>
而是List<Dictionary<string, object>>
。
我使用 dynamic 來回答您的問題,因為您的問題中沒有類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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.