-1

I am currently attempting to build an ASP.NET Core API.

On startup it checks if there is anything in its EF Core powered DB. If not it reads a few JSON files and fills it up. To do that it needs to Deserialize them. One of the properties is a List of strings and EF Core doesn't like that. To get around it I made a dummy Model.

public class StringModel
{
    public int id { get; set; }
    public string str { get; set; }
}

And the other Model.

public class Book
{
    public int id { get; set; }
    public virtual List<StringModel> chapters { get; set; }
}

This is not how my models look. This is a minimum reproducible example.

JSON

[{
  chapters: ["a","b"]
}, {
  chapters: ["foo","bar"]
}]

And this is where it throws

string json = GetJSONFromFile();
var objs = JsonConvert.DeserializeObject<List<Book>>(json);

Newtonsoft.Json.JsonSerializationException: 'Error converting value "a" to type 'Project.Models.Primitives.StringModel'. Path '[0].chapters[0]'

Any ideas?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • The json you have supplied doesn't match the Book class. Where is the `id` property? – Barry O'Kane Apr 28 '22 at 15:22
  • Why not just reshuffle the JSON so that a chapter is an entity - that way it's a list of chapters, not of strings – Andrew Corrigan Apr 28 '22 at 15:49
  • Hi @bplatanasov, your `chapters` is a list model, but in your json string it is a list string property. If you do not want to change the model design, you need change the json string. If your json string cannot be changed,you may need custom json deserializer. Besides, you need tell us which property in `StringModel` match the item in array(`["a","b"]`). – Rena Apr 29 '22 at 01:44
  • Hello @Rena. I originally wanted to use List, but since I can't create a DB with a list of primitive types I made this dummy object to create a table in the DB. As for matching every string the array should create a new StringModel and a new entry in the DB. I found [this](https://stackoverflow.com/questions/54313882/c-sharp-entity-framework-json-deserialize-string-array-issues), but I do not wish to use comma separation. I can change the model design, but cannot change the JSON sadly. If it can't be helped how do I go about implementing a custom deserializer? – bplatanasov May 06 '22 at 15:00

1 Answers1

-1

for your json you need this class

     public class Book
    {
        public int id { get; set; }
        public List<string> chapters { get; set; }
    }

and after this you can use

var objs = JsonConvert.DeserializeObject<List<Book>>(json);

if you want to use StringModel instead of string , your json should be

[
{"id":5,"chapters":[{"id":1,"str":"a"},{"id":2,"str":"b"}]},
{"id":6,"chapters":[{"id":3,"str":"bar"},{"id":4,"str":"foo"}]}
]
Serge
  • 40,935
  • 4
  • 18
  • 45
  • This won't work. I am using the code first approach when creating the DB with EF Core and it doesn't work with List, but it does List. The problem is the Deserialization. – bplatanasov Apr 28 '22 at 15:42
  • @bplatanasov These both jsons are tested and working propery. You should post the real json you have . – Serge Apr 28 '22 at 15:47
  • +1 for giving me a clue how to solve this. The Deserializaer doesn't understand my StringModel class and List, as stated above, doesn't work with EF Core. Changing the JSON to an array of object with {"str": "foo"} seems to work. I would prefer not to go through the entire file, but if I have to I will. – bplatanasov Apr 28 '22 at 16:00