I have the following json object that is available in my .ashx handler (var items=):
我有我的.ashx处理程序中可用的以下json对象(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?
当我使用javascript序列化程序时,它会返回我的AdditionalCoveragesPackage对象 - 但是 - 两个属性:Coverages和CombinedMaximums都是空的。如何正确反序列化我的json字符串?
3 个解决方案
#1
3
In your JSON string Coverages
and CoverageCombinedMaximums
are simple objects, not arrays, so:
在您的JSON字符串中Coverages和CoverageCombinedMaximums是简单对象,而不是数组,因此:
public class AdditionalCoveragesPackage
{
public AdditionalCoverage Coverages { get; set; }
public AdditionalCoverageCombinedMaximum CoverageCombinedMaximums { get; set; }
}
Also in your example JSON:
同样在您的示例JSON中:
"MaximumPerAnnum":""
so make sure you define the corresponding property as a nullable decimal:
所以请确保将相应的属性定义为可以为null的小数:
public decimal? MaximumPerAnnum { get; set; }
or the deserializer will blow.
或者解串器会吹。
#2
2
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.
JSON中的Coverages和CombinedMaximums属性是对象,而不是数组。您应该将这些更改为数组或更改您的C#类以不使用List
#3
0
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:
SLAKS和Darin Dimitrov都帮助了很多。如果我将我的JSON字符串组成如下(我的数组结构不正确并且为我的数字类型传递空字符串而不是零),它结束它是有效的。这是更正的客户端代码:
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);
#1
3
In your JSON string Coverages
and CoverageCombinedMaximums
are simple objects, not arrays, so:
在您的JSON字符串中Coverages和CoverageCombinedMaximums是简单对象,而不是数组,因此:
public class AdditionalCoveragesPackage
{
public AdditionalCoverage Coverages { get; set; }
public AdditionalCoverageCombinedMaximum CoverageCombinedMaximums { get; set; }
}
Also in your example JSON:
同样在您的示例JSON中:
"MaximumPerAnnum":""
so make sure you define the corresponding property as a nullable decimal:
所以请确保将相应的属性定义为可以为null的小数:
public decimal? MaximumPerAnnum { get; set; }
or the deserializer will blow.
或者解串器会吹。
#2
2
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.
JSON中的Coverages和CombinedMaximums属性是对象,而不是数组。您应该将这些更改为数组或更改您的C#类以不使用List
#3
0
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:
SLAKS和Darin Dimitrov都帮助了很多。如果我将我的JSON字符串组成如下(我的数组结构不正确并且为我的数字类型传递空字符串而不是零),它结束它是有效的。这是更正的客户端代码:
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);