*** Edit: Clarified there are two, separate JSON files ***
I have two classes:
public class Phone
{
public int PhoneId { get; set; }
public string Name { get; set; }
public Manufacturer PhoneManufacturer { get; set; }
}
public class Manufacturer
{
public int ManId { get; set; }
public string Name { get; set; }
}
And two JSON files containing data for both classes:
phones.json:
[
{
"phoneId" : 45,
"name": "S20",
"phoneManufacturer":16
}
]
manufacturers.json:
[
{
"manId" : 16,
"name": "Samsung"
}
]
I use the following code to deserialize the phones objects:
string jsonString = File.ReadAllText("phones.json");
return JsonSerializer.Deserialize<List<Phone>>(jsonString, new JsonSerializerOptions { PropertyNameCaseInsensitive = true})!;
I want that when deserializing the Phones JSON to List<Phone>
, the resulting objects will have its PhoneManufacturer
property populated with the actual Manufacturer
object.
When running the deserialization with the default parameters, I get an error that JSON cannot convert the value in the PhoneManufacturer
property to a Manufacturer
object, which makes sense since this is the object Id, and not the object itself.
How can I achieve that?