当我执行以下命令时,我的 JSON 字符串的重复部分没有被反序列化:
UsageAndDemand.UsageAndDemandResponse UsgDmdResp = JsonConvert.DeserializeObject<UsageAndDemand.UsageAndDemandResponse>(jsonString);
该代码在我的另一个使用不同对象的程序中正常工作
这是我的代码:
downloadString =
"http://dp72m.someserver.com:9090/ws/simple/queryMeterUsage?";
downloadString += "startdate=" + startDate + "&enddate=" + endDate +
"&profile=" + profile + "&metric=";
downloadString += metric;
downloadString += "&sdp=" + sdp + "&zip=" + zipCode + "&metertype=" +
meterType;
using (var client = new WebClient())
{
jsonString = client.DownloadString(downloadString);
}
UsageAndDemand.UsageAndDemandResponse UsgDmdResp =
JsonConvert.DeserializeObject<UsageAndDemand.UsageAndDemandResponse>
(jsonString);
if (UsgDmdResp.ResponseCode == 0)
{
PopulateGraph(UsgDmdResp);
类定义:
public class UsageAndDemand
{
}
public UsageAndDemand()
{
//
// TODO: Add constructor logic here
//
}
public class MeterUsageDemand
{
[JsonProperty("UsageDate", NullValueHandling =
NullValueHandling.Ignore)]
public String UsageDate { get; set; }
[JsonProperty("KwhUsed", NullValueHandling =
NullValueHandling.Ignore)]
public float KwhUsed { get; set; }
[JsonProperty("HighTemp", NullValueHandling =
NullValueHandling.Ignore)]
public float MaxTemp { get; set; }
[JsonProperty("LowTemp", NullValueHandling =
NullValueHandling.Ignore)]
public float MinTemp { get; set; }
[JsonProperty("KwDemand", NullValueHandling =
NullValueHandling.Ignore)]
public float KwDemand { get; set; }
}
public class UsageAndDemandResponse
{
public int ResponseCode { get; set; }
public string Message { get; set; }
public List<MeterUsageDemand> MeterUsageDemand { get; set; }
}
要反序列化的 JSON 字符串:
{
"ResponseCode" : 0,
"Message" : "Success",
"UsageAndDemand" : [
{
"UsageDate" : "2018-08-01",
"KwhUsed" : 624.27,
"HighTemp" : 93.00,
"LowTemp" : 70.00,
"KwDemand" : 1.21
},
{
"UsageDate" : "2018-09-01",
"KwhUsed" : 777.75,
"HighTemp" : 93.00,
"LowTemp" : 68.00,
"KwDemand" : 1.12
},
{
"UsageDate" : "2018-10-01",
"KwhUsed" : 815.52,
"HighTemp" : 91.00,
"LowTemp" : 50.00,
"KwDemand" : 1.28
},
{
"UsageDate" : "2018-11-01",
"KwhUsed" : 844.68,
"HighTemp" : 87.00,
"LowTemp" : 36.00,
"KwDemand" : 1.3
},
{
"UsageDate" : "2018-12-01",
"KwhUsed" : 800.8,
"HighTemp" : 81.00,
"LowTemp" : 41.00,
"KwDemand" : 1.27
},
{
"UsageDate" : "2019-01-01",
"KwhUsed" : 27.8,
"KwDemand" : 1.22
}
]
}
在VS2015中的调试器中,如果我检查反序列化对象(UsgDmdResp),我可以看到ResponseCode
等于0,并且消息显示"成功",但重复部分(JSON字符串)为空。正如我上面提到的,代码正在另一个页面中工作,该页面反序列化为稍微不同的对象类。
谁能看出为什么我的重复值为空?
应该UsageAndDemand
错误的属性名称MeterUsageDemand
public class UsageAndDemandResponse
{
public int ResponseCode { get; set; }
public string Message { get; set; }
[JsonProperty("UsageAndDemand", NullValueHandling = NullValueHandling.Ignore)]
public List<MeterUsageDemand> MeterUsageDemand { get; set; }
}
或
public class UsageAndDemandResponse
{
public int ResponseCode { get; set; }
public string Message { get; set; }
public List<MeterUsageDemand> UsageAndDemand{ get; set; }
}