1

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!

1
  • Your Raw JSON has only one element and you are trying to put it in list, then you deserialize it as a single Session object and then push it into the ObservableCollection. In a way you are trying to deserialize a single object to List which is technically wrong. Commented Sep 20, 2013 at 13:54

1 Answer 1

1

Your JSON represents only a single element, so you must deserialize it into a single object. In order to deserialize directly into a collection, your JSON would need to represent an array. Since it does not, you are getting an error when you try to do this.

Your best bet is to deserialize the JSON into a Session object like you were doing originally, then simply create an ObservableCollection yourself and add the Session to it.

Session session = JsonConvert.DeserializeObject<Session>(sessionResponse);
ObservableCollection collection = new ObservableCollection<Session>();
collection.Add(session);
Sign up to request clarification or add additional context in comments.

1 Comment

That fixed it and I learnt a bit more about parsing JSON. Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.