I have the following json object that is available in my .ashx handler (var items=):
{"Coverages":{"PersonID":10,"DetCode":"","Reimbursement":"","Deductible":"","MaximumPerAnnum":"","MaximumPerVisit":"","MaximumPerVisits":"","SvcCode":""},"CoverageCombinedMaximums":{"PersonID":10,"DetCode":["AAAAA","BBBBB","CCCCC"],"MaximumPerAnnum":""}}
public void ProcessRequest (HttpContext context) {
bool isSuccessful = true;
var items = context.Request.Params["items"];
if (isSuccessful)
{
JavaScriptSerializer ser = new JavaScriptSerializer();
AdditionalCoveragesPackage package = ser.Deserialize<AdditionalCoveragesPackage>(items);
}
else
{
SendErrorMessage(context);
return;
}
}
Here is the structure of the class I'm trying to deserialize:
public class AdditionalCoverage
{
public int PersonID { get; set; }
public string DetCode { get; set; }
public decimal? Reimbursement { get; set; }
public decimal? Deductible { get; set; }
public decimal? MaximumPerAnnum { get; set; }
public decimal? MaximumPerVisit { get; set; }
public int? MaximumPerVisits { get; set; }
public string SvcCode { get; set; }
public AdditionalCoverage()
{
}
}
public class AdditionalCoverageCombinedMaximum
{
public int PersonID { get; set; }
public string SvcCode { get; set; }
public decimal? MaximumPerAnnum { get; set; }
public List<string> DetCode { get; set; }
public AdditionalCoverageCombinedMaximum()
{
}
}
public class AdditionalCoveragesPackage
{
public List<AdditionalCoverage> Coverages { get; set; }
public List<AdditionalCoverageCombinedMaximum> CoverageCombinedMaximums { get; set; }
public AdditionalCoveragesPackage()
{
}
public AdditionalCoveragesPackage(AdditionalCoverage coverage, AdditionalCoverageCombinedMaximum maximum)
{
List<AdditionalCoverage> coverages = new List<AdditionalCoverage>();
coverages.Add(coverage);
Coverages = coverages;
List<AdditionalCoverageCombinedMaximum> maximums = new List<AdditionalCoverageCombinedMaximum>();
maximums.Add(maximum);
CoverageCombinedMaximums = maximums;
}
public AdditionalCoveragesPackage(List<AdditionalCoverage> coverages, List<AdditionalCoverageCombinedMaximum> maximums)
{
Coverages = coverages;
CoverageCombinedMaximums = maximums;
}
}
Edit: Here are my client side methods that need correction:
function saveCoverageDetails() {
var handler = "HttpHandlers/UpdateAdditionalCoverageDetailsHandler.ashx";
var coverages = { PersonID: personId, DetCode: "", Reimbursement: "", Deductible: "", MaximumPerAnnum: "", MaximumPerVisit: "", MaximumPerVisits: "", SvcCode: "" };
var maximums = { PersonID: personId, DetCode: ["ACUPUN", "PODIAT", "OSTEOP"], MaximumPerAnnum: "" };
var obj = { Coverages: coverages, CoverageCombinedMaximums: maximums };
var data = ({ items: JSON.stringify(obj) });
callHandler(handler, data, saveSuccessful, failure);
}
function callHandler(handler, obj, onSuccess, onFail) {
$.ajax({
type: "POST",
url: handler,
data: obj,
dataType: "json",
success: onSuccess,
fail: onFail
});
}
When I'm using the javascript serializer it does return my AdditionalCoveragesPackage object - however - both of the properties: Coverages and CombinedMaximums are empty. How to get this to properly deserialize my json string?
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);
来源:https://stackoverflow.com/questions/7890557/how-to-properly-use-javascript-deserialization-to-convert-a-json-string-to-a-com