Mam problem z następującym JSON podczas deserializacji przy użyciu JSON.NET.Deserialize JSON nie działa z JSON.NET
{
"?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"
}]
}
}]
}
}
Używam następujących klas:
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<Person> Person { get; set; }
}
public class Skill
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Skills
{
public List<Skill> 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; }
}
Kiedy próbuję deserializowania
RootObject persons = JsonConvert.DeserializeObject<RootObject>(json);
mam następujący błąd:
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.
Przypuszczam problem występuje w notacji:
Czego mi brakuje, czy istnieje proste rozwiązanie tego problemu?
UPDATE:
więc ostatecznie problemem było to, że czasami tablicą JSON
"Skills": {
"Skill": [{
a czasem obiekt JSON
"Skills": {
"Skill": {
Ale podczas wklejania/sprawdzając mój kod w walidatorach będzie zawsze sformatowany jako tablica JSON, więc sprawdziłem go za pomocą okna oglądania, aby zobaczyć surowy ciąg znaków json.
Stamtąd łatwo było oznaczyć nieruchomość z JsonConverter przypisują
public class Skills
{
[JsonConverter(typeof(MyConverter))]
public List<Skill> Skill { get; set; }
}
i napisać konwerter:
public class MyConverter : JsonConverter
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartArray)
{
return serializer.Deserialize<List<Skill>>(reader);
}
else
{
Skill skill = serializer.Deserialize<Skill>(reader);
return new List<Skill>(new[] { skill});
}
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(value);
}
}
nadzieję, że pomaga kogoś.
Czytanie json z pliku, a deserializacji z kodem, wszystko działa dobrze dla mnie. – Pondidum
Zaktualizowałem moje pytanie z odpowiedzią. –
Fantastyczna odpowiedź. Powinieneś oznaczyć go jako zaakceptowaną odpowiedź. Idealne dla mnie. Jedyną zmianą, którą wprowadziłem, było stworzenie ogólnego konwertera zamiast twardego kodowania dla dowolnego typu (umiejętność w twoim przykładzie). –