I am attempting to deserialize a JSON string into an ObservableCollection object but Json.net is throwing this error
{"Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.ObjectModel.ObservableCollection`1[ZenPanda.DataModel.Session]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\r\nPath 'arguments', line 1, position 13."}
My Datamodel is below
public class Session
{
[JsonProperty("arguments")]
public SessionProperties arguments { get; set; }
[JsonProperty("result")]
public string Result { get; set; }
[JsonProperty("tag")]
public int Tag { get; set; }
}
public class SessionProperties
{
[JsonProperty("alt-speed-down")]
public int Altspeeddown { get; set; }
[JsonProperty("alt-speed-enabled")]
public bool Altspeedenabled { get; set; }
[JsonProperty("alt-speed-time-begin")]
public int Altspeedtimebegin { get; set; }
[JsonProperty("alt-speed-time-day")]
public int Altspeedtimeday { get; set; }
[JsonProperty("alt-speed-time-enabled")]
public bool Altspeedtimeenabled { get; set; }
[JsonProperty("units")]
public SessionUnits Units { get; set; }
[JsonProperty("utp-enabled")]
public bool Utpenabled { get; set; }
}
public class SessionUnits
{
[JsonProperty("memory-bytes")]
public int Memorybytes { get; set; }
[JsonProperty("memory-units")]
public List<string> Memoryunits { get; set; }
}
This is the code calling JsonConvert
public ObservableCollection<Session> currentSession = new ObservableCollection<Session>();
string sessionResponse = await task.Content.ReadAsStringAsync();
currentSession = JsonConvert.DeserializeObject<ObservableCollection<Session>>(sessionResponse);
This is raw JSON
{"arguments": {"alt-speed-down":50,"alt-speed-enabled":false,"alt-speed-time-begin":540,"alt-speed-time-day":127,"alt-speed-time-enabled":false,
"units":{"memory-bytes":1024,"memory-units":["KiB","MiB","GiB","TiB"],"size-bytes":1000,"size-units":["kB","MB","GB","TB"],"speed- bytes":1000,"speed-units":["kB/s","MB/s","GB/s","TB/s"]},
"utp-enabled":true},
"result":"success",
"tag":568}
If I declare currentSession as a ordinary Session object then Json.net happily deserializes into that instance but when I declare it as ObservableCollection Json.net throws an error.
I am quite new to programming so apologies if this is a complete newbie cockup question/problem. Thanks in advance!