Asked by:
trouble with deserialize json string

Question
-
User450763225 posted
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; } }
Tuesday, May 21, 2019 1:13 AM
All replies
-
User283571144 posted
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.
{"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"}}}}
C# codes:
EmailModels m1 = new EmailModels() { insurance = new Insurance() { individual= new InsIndividual() { amount = "", Email ="", FullName= "", registrationID= "" }, multi = new InsMulti() { amount = "132", borRegistrationID= "111111", emailList = new Dictionary<string, string>() { { "2391734", "jfowler@thepelicangroup.ca" },{ "4773998", "edaho.properties@gmail.com" } }, paidFor = new Dictionary<string, string>() { { "2391734","44" }, { "4773998" , "44" } } } } }; string json = JsonConvert.SerializeObject(m1);
Best Regards,
Brando
Tuesday, May 21, 2019 5:13 AM -
User753101303 posted
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...
Tuesday, May 21, 2019 9:05 AM