How to properly use javascript deserialization to convert a json string to a complex object? How to properly use javascript deserialization to convert a json string to a complex object? json json

How to properly use javascript deserialization to convert a json string to a complex object?


In your JSON string Coverages and CoverageCombinedMaximums are simple objects, not arrays, so:

public class AdditionalCoveragesPackage{    public AdditionalCoverage Coverages { get; set; }    public AdditionalCoverageCombinedMaximum CoverageCombinedMaximums { get; set; }}

Also in your example JSON:

"MaximumPerAnnum":""

so make sure you define the corresponding property as a nullable decimal:

public decimal? MaximumPerAnnum { get; set; }

or the deserializer will blow.


The Coverages and CombinedMaximums properties in your JSON are objects, not arrays.
You should change those to arrays or change your C# class to not use List<T>s.


SLAKS and Darin Dimitrov both helped a ton. Ends up it works if I compose my JSON string as below (I had the array structured incorrectly and was passing empty strings instead of zeros for my numeric types). Here's the corrected client code:

 var handler = "HttpHandlers/UpdateAdditionalCoverageDetailsHandler.ashx";                var coverages = [{ PersonID: personId, DetCode: "", Reimbursement: 0, Deductible: 0, MaximumPerAnnum: 0, MaximumPerVisit: 0, MaximumPerVisits: 0, SvcCode: "" }, { PersonID: personId, DetCode: "CHIROP", Reimbursement: 0, Deductible: 0, MaximumPerAnnum: 0, MaximumPerVisit: 0, MaximumPerVisits: 0, SvcCode: ""}];                var maximums = [{ PersonID: personId, DetCode: ["ACUPUN", "PODIAT", "OSTEOP"], MaximumPerAnnum: 0 }, { PersonID: personId, DetCode: ["ACUPUN", "PODIAT", "OSTEOP"], MaximumPerAnnum: 0}];                var obj = { Coverages: coverages, CoverageCombinedMaximums: maximums };                var data = ({ items: JSON.stringify(obj) });                callHandler(handler, data, saveSuccessful, failure);