[英]Newtonsoft.Json.JsonSerializationException: Error converting value, Could not cast or convert from System.String to DashBoard.DashboardReport
I have written the code to deserialize JSON from String to Class(DashBoardReport) Object type,but it gives me below exception, I checked out my JSON structure, it is a valid JSON structure and also it is matching with the DashBoardReport Class structure, so I don't understand why its giving me the cast related exception,please help.我已经编写了将 JSON 从 String 反序列化为 Class(DashBoardReport) 对象类型的代码,但它给了我以下异常,我检查了我的 JSON 结构,它是一个有效的 JSON 结构,并且它与 DashBoardReport 类结构匹配,所以我不明白为什么它给了我与演员相关的异常,请帮忙。
Complete Exception:完全异常:
Newtonsoft.Json.JsonSerializationException: Error converting value "{"Company":"Shyamlal Bros","VoucherType":"PurcOrder","Interval":null,"CurrentPeriodSummary":{"StartDate":"20170401","EndDate":"20170430","Amount":2459250.0,"Monthlygross":0.0},"PreviousPeriodSummary":{"StartDate":null,"EndDate":null,"Amount":0.0,"Monthlygross":0.0},"CurrentPeriodNetAmount":" ","CurrentPeriodDetails":[{"StartDate":"1-Apr-2017","EndDate":"30-Apr-2017","Amount":2459250.0,"Monthlygross":0.0}],"PreviousPeriodDetails":[],"GrowthIndicator":null,"Growth":0.0,"GroupByReport":null,"Error":null}" to type 'DashBoard.DashboardReport'. Path '', line 1, position 570. ---> System.ArgumentException: Could not cast or convert from System.String to DashBoard.DashboardReport.
Code For Deserialization :反序列化代码:
string json="{\"Company\":\"Shyamlal Bros\",\"VoucherType\":\"PurcOrder\",\"Interval\":null,\"CurrentPeriodSummary\":{\"StartDate\":\"20170401\",\"EndDate\":\"20170430\",\"Amount\":2459250.0,\"Monthlygross\":0.0},\"PreviousPeriodSummary\":{\"StartDate\":null,\"EndDate\":null,\"Amount\":0.0,\"Monthlygross\":0.0},\"CurrentPeriodNetAmount\":\"\",\"CurrentPeriodDetails\":[{\"StartDate\":\"1-Apr-2017\",\"EndDate\":\"30-Apr-2017\",\"Amount\":2459250.0,\"Monthlygross\":0.0}],\"PreviousPeriodDetails\":[],\"GrowthIndicator\":null,\"Growth\":0.0,\"GroupByReport\":null,\"Error\":null}";
DashboardReport r=JsonConvert.DeserializeObject<DashboardReport>(json);
string jsonMonth = JsonConvert.SerializeObject(r);
Console.WriteLine(jsonMonth);
Beutified JSON Structure ie to be deserialized: Beutified JSON 结构,即要反序列化:
{
"Company": "Shyamlal Bros",
"VoucherType": "PurcOrder",
"Interval": null,
"CurrentPeriodSummary": {
"StartDate": "20170401",
"EndDate": "20170430",
"Amount": 2459250,
"Monthlygross": 0
},
"PreviousPeriodSummary": {
"StartDate": null,
"EndDate": null,
"Amount": 0,
"Monthlygross": 0
},
"CurrentPeriodNetAmount": " ",
"CurrentPeriodDetails": [
{
"StartDate": "1-Apr-2017",
"EndDate": "30-Apr-2017",
"Amount": 2459250,
"Monthlygross": 0
}
],
"PreviousPeriodDetails": [],
"GrowthIndicator": null,
"Growth": 0,
"GroupByReport": null,
"Error": null
}
DashboardReport Class structure: DashboardReport 类结构:
public class DashboardReport:ITallyReport
{
private string company;
private string voucherType;
private string interval;
private string error;
private DashboardReportElement currentPeriodSummary;
private string currentPeriodNetAmount;
private DashboardReportElement previousPeriodSummary;
private DashboardReportElement[] currentPeriodDetails;
private DashboardReportElement[] previousPeriodDetails;
public DashboardReport()
{
currentPeriodSummary = new DashboardReportElement();
previousPeriodSummary = new DashboardReportElement();
currentPeriodDetails = new DashboardReportElement[] { };
previousPeriodDetails = new DashboardReportElement[] { };
}
public string Company
{
get
{
return company;
}
set
{
company = value;
}
}
public string VoucherType
{
get
{
return voucherType;
}
set
{
voucherType = value;
}
}
public string Interval
{
get
{
return interval;
}
set
{
interval = value;
}
}
public DashboardReportElement CurrentPeriodSummary
{
get
{
return currentPeriodSummary;
}
set
{
currentPeriodSummary = value;
}
}
public DashboardReportElement PreviousPeriodSummary
{
get
{
return previousPeriodSummary;
}
set
{
previousPeriodSummary = value;
}
}
public string CurrentPeriodNetAmount { get => currentPeriodNetAmount; set => currentPeriodNetAmount = value; }
public DashboardReportElement[] CurrentPeriodDetails
{
get
{
return currentPeriodDetails;
}
set
{
currentPeriodDetails = value;
}
}
public DashboardReportElement[] PreviousPeriodDetails
{
get
{
return previousPeriodDetails;
}
set
{
previousPeriodDetails = value;
}
}
public string GrowthIndicator { get; set; }
public double Growth { get; set; }
public DashboardGroupByReport GroupByReport { get; set; }
public string Error { get => error; set => error = value; }
//public ReportElement[] Data { get; set; }
//public string GroupBy { get; set; }
public string ToJson()
{
return JsonConvert.SerializeObject(this);
}
}
Structure of DashBoardReportElement Class: DashBoardReportElement 类的结构:
public class DashboardReportElement
{
private string startDate;
private string endDate;
private double amount;
private double monthlygross;
public DashboardReportElement()
{
}
public string StartDate
{
get
{
return startDate;
}
set
{
startDate = value;
}
}
public string EndDate
{
get
{
return endDate;
}
set
{
endDate = value;
}
}
public Double Amount
{
get
{
return amount;
}
set
{
amount = value;
}
}
public double Monthlygross { get => monthlygross; set => monthlygross = value; }
}
Most likely your exception is coming from a bad-formatted JSON string.您的异常很可能来自格式错误的 JSON 字符串。
Your example code works nicely.您的示例代码运行良好。 If we take a smaller example:
如果我们举一个较小的例子:
string json="{\"Company\":\"Shyamlal Bros\"}";
It will be serialised again into your class without troubles, just like the string from your example.它将毫无问题地再次序列化到您的类中,就像您示例中的字符串一样。 But if you have it with extra quotation:
但如果你有额外的报价:
string json="\"{\"Company\":\"Shyamlal Bros\"}\"";
You are in trouble.你有麻烦了。 So the solution is to trim those:
所以解决方案是修剪那些:
json = json.Trim("\"".ToCharArray());
As a side note - in your example there is no need for a field-backed properties, your class can be easily rewritten as作为旁注 - 在您的示例中,不需要字段支持的属性,您的类可以轻松重写为
public class DashboardReport
{
public string Company { get; set; }
public string VoucherType { get; set; }
public string Interval { get; set; }
public DashboardReportElement CurrentPeriodSummary { get; set; }
public DashboardReportElement PreviousPeriodSummary { get; set; }
public string CurrentPeriodNetAmount { get; set; }
public DashboardReportElement[] CurrentPeriodDetails { get; set; }
public DashboardReportElement[] PreviousPeriodDetails { get; set; }
public string GrowthIndicator { get; set; }
public double Growth { get; set; }
public DashboardGroupByReport GroupByReport { get; set; }
public string Error { get; set; }
public string ToJson()
{
return JsonConvert.SerializeObject(this);
}
}
i think the json you have provided here is correct but the json which you are using in your actual code is bad.我认为您在此处提供的 json 是正确的,但您在实际代码中使用的 json 是错误的。 You may be using json as
您可能正在使用 json 作为
string json="\"{\"Company\":\"Shyamlal Bros\",\"VoucherType\":\"PurcOrder\",\"Interval\":null,\"CurrentPeriodSummary\":{\"StartDate\":\"20170401\",\"EndDate\":\"20170430\",\"Amount\":2459250.0,\"Monthlygross\":0.0},\"PreviousPeriodSummary\":{\"StartDate\":null,\"EndDate\":null,\"Amount\":0.0,\"Monthlygross\":0.0},\"CurrentPeriodNetAmount\":\"\",\"CurrentPeriodDetails\":[{\"StartDate\":\"1-Apr-2017\",\"EndDate\":\"30-Apr-2017\",\"Amount\":2459250.0,\"Monthlygross\":0.0}],\"PreviousPeriodDetails\":[],\"GrowthIndicator\":null,\"Growth\":0.0,\"GroupByReport\":null,\"Error\":null}\"";
// Extra Quatation Marks in json
Is Your DashboardReportElement look like this ?你的 DashboardReportElement 看起来像这样吗?
public class DashboardReportElement
{
public string StartDate { get; set; }
public string EndDate { get; set; }
public int Amount { get; set; }
public int Monthlygross { get; set; }
}
Use List<DashboardReportElement>
as a return type Instead of DashboardReportElement[]
array使用
List<DashboardReportElement>
作为返回类型而不是DashboardReportElement[]
数组
Ex -前任 -
private DashboardReportElement currentPeriodSummary;
private string currentPeriodNetAmount;
private DashboardReportElement previousPeriodSummary;
private List<DashboardReportElement> currentPeriodDetails; // made List instead of array
private List<DashboardReportElement> previousPeriodDetails; // made List instead of Array
I got this error when requesting application/json
:请求
application/json
时出现此错误:
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
If I remove the header I received correct json.如果我删除标题,我会收到正确的 json。 (I guess it is a misconfiguration at the server-side, but at te moment I've got bigger fish to fry.)
(我想这是服务器端的配置错误,但现在我有更大的鱼要煎。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.