Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserialize JSON not working with JSON.NET
    primarykey
    data
    text
    <p>I have a problem with the following JSON when deserializing using JSON.NET.</p> <pre><code>{ "?xml": { "@version": "1.0", "@encoding": "utf-8" }, "Persons": { "Person": [{ "@Id": "1", "@Name": "John", "@Surname": "Smith" }, { "@Id": "2", "@Name": "John", "@Surname": "Smith", "Skills": { "Skill": [{ "@Id": "1", "@Name": "Developer" }, { "@Id": "2", "@Name": "Tester" }] } }] } } </code></pre> <p>I'm using the following classes:</p> <pre><code>public class RootObject { public Xml xml { get; set; } public Persons Persons { get; set; } } public class Xml { public string version { get; set; } public string encoding { get; set; } } public class Persons { public List&lt;Person&gt; Person { get; set; } } public class Skill { public int Id { get; set; } public string Name { get; set; } } public class Skills { public List&lt;Skill&gt; Skill { get; set; } } public class Person { public int Id { get; set; } public string Name { get; set; } public string Surname { get; set; } public Skills Skills { get; set; } } </code></pre> <p>When i try to deserialize</p> <pre><code>RootObject persons = JsonConvert.DeserializeObject&lt;RootObject&gt;(json); </code></pre> <p>i got the following error:</p> <blockquote> <p>Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Project.Models.Persons.Skill]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To 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.</p> </blockquote> <p>I suppose the problem is in the notation:</p> <pre><code>"Skills": { "Skill": [{ </code></pre> <p>What am I missing, is there an easy solution to this problem?</p> <p><strong>UPDATE:</strong></p> <p>So finally the problem was that it was sometimes a JSON array</p> <pre><code>"Skills": { "Skill": [{ </code></pre> <p>and sometimes a JSON object</p> <pre><code>"Skills": { "Skill": { </code></pre> <p>But when pasting/checking my code into validators it would always be formatted as a JSON array so i've inspected it using watch window to see the raw json string.</p> <p>From there it was easy to mark the property with a JsonConverter attribute</p> <pre><code>public class Skills { [JsonConverter(typeof(MyConverter))] public List&lt;Skill&gt; Skill { get; set; } } </code></pre> <p>and write the converter:</p> <pre><code>public class MyConverter : JsonConverter { public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.StartArray) { return serializer.Deserialize&lt;List&lt;Skill&gt;&gt;(reader); } else { Skill skill = serializer.Deserialize&lt;Skill&gt;(reader); return new List&lt;Skill&gt;(new[] { skill}); } } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { writer.WriteValue(value); } } </code></pre> <p>Hope it helps somebody.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload