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

自古美人都是妖i 提交于 2019-12-04 14:02:19

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.

FiveTools

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);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!