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<EmailModels>(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; }
}
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
It doesn't match. Your JSON is an array but you try to deserialize this to a single object. Change [] to {} or deserialize to a List<EmailModels> depending on which one you really want...
Member
124 Points
219 Posts
trouble with deserialize json string
May 21, 2019 01:13 AM|sdnd2000|LINK
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."
Star
9591 Points
3012 Posts
Re: trouble with deserialize json string
May 21, 2019 05:13 AM|Brando ZWZ|LINK
Hi sdnd2000,
According to your description and error message, I guess there are something wrong with your json string.
I suggest you could firstly new a EmailModels, then you could use JsonConvert.SerializeObject method to convert the model to json string.
Then you could compare the json string with your pervious json string.
Here is the right json which work well.
C# codes:
Best Regards,
Brando
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
All-Star
45870 Points
16703 Posts
Re: trouble with deserialize json string
May 21, 2019 09:05 AM|PatriceSc|LINK
Hi,
It doesn't match. Your JSON is an array but you try to deserialize this to a single object. Change [] to {} or deserialize to a List<EmailModels> depending on which one you really want...