I have got an error:
Cannot deserialize the current JSON array (eg [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,Tessa.Extensions.Shared.Acquiring.AcqFieldData]' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly.
when i pass a JSON array string (settings) to the JsonConvert.DeserializeObject<Dictionary<String, AcqFieldData>>(settings)
and the other error:
Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.KeyValuePair`2[System.String,Tessa.Extensions.Shared.Acquiring.AcqFieldRef][]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly.
when i pass a JSON object string as an input. Here is my two inputs, array:
[
{
"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"
]
}
}
]
and object:
{
"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"
]
}
}
Here is my classes. 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);
}
}
}
and 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()
{
}
}
First json you have is not a Dictionary<string, object>
but a List<Dictionary<string, object>>
.
I used dynamic to answer your question since there was no definition of the class, AcqFieldData
in your question.
var value = JsonConvert.DeserializeObject<List<Dictionary<string, dynamic>>>(json);
and your Second json is Dictionary<string, object>
.
var value = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(json);
This code works with dynamic object but not AcqFieldData
. Your constructors seems to have some issues when trying to create / add objects and inserting them to your Dictionary. When I tested your code, it throws error in foreach (var pair in fields)
when fields is null.
I would suggest trying out classes with getter and setter and go from there.
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.