Deserialize json string

Hi, I got some trouble with deserialize json string with class, I got this error :

" Cannot deserialize the current JSON array (e.g. [1,2,3]) into type ‘JsonTest.EmailModels’ because the type requires a JSON object (e.g. {“name”:“value”}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {“name”:“value”}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array."

        static void Main(string[] args)
    {
        string json = @"[

‘insurance’:{
‘individual’:{
‘registrationID’:’’,
‘amount’:’’,
‘Fullname’:’’,
‘Email’:’’
},
‘multi’:{
‘borRegistrationID’:‘111111’,
‘amount’:‘132’,
‘paidfor’:{
‘2391734’:‘44’,
‘4773998’:‘44’
},
‘emailList’:{
‘2391734’:‘jfowler@thepelicangroup.ca’,
‘4773998’:‘edaho.properties@gmail.com’
}
}
}

]";
EmailModels emailModel = JsonConvert.DeserializeObject(json);
foreach (var item in emailModel.insurance.multi.paidFor)
{
Console.WriteLine(item.Key);
Console.WriteLine(item.Value);
}

    }
}
public class EmailModels
{
    public Insurance insurance { get; set; }
}

public class Insurance
{
    public InsIndividual individual { get; set; }
    public InsMulti multi { get; set; }
}

public class InsIndividual
{
    public string registrationID { get; set; }
    public string amount { get; set; }
    public string FullName { get; set; }
    public string Email { get; set; }
}
public class InsMulti
{
    public string borRegistrationID { get; set; }
    public string amount { get; set; }
    public Dictionary<string, string> paidFor { get; set; }
    public Dictionary<string, string> emailList { get; set; }
}

This kind of question is better asked at https://stackoverflow.com/

You can use https://www.newtonsoft.com/json/help/html/Introduction.htm for that

There are many exemples :slight_smile:

You have a list of emails.

In that class you can add List<string> emails, I think.

Also this can help you
https://app.quicktype.io/

Good luck ! :slight_smile:

1 Like
.NET Foundation Website | Blog | Projects | Code of Conduct