无法使用newtonsoft反序列化为json字符串

时间:2019-01-03 17:09:54

标签: c# json.net

执行此命令时,不会反序列化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字符串)一片空白。如前所述,该代码正在另一个反序列化为稍微不同的对象类的页面中工作。

谁能看到我的重复值为何为空?

1 个答案:

答案 0 :(得分:5)

错误的属性名称MeterUsageDemand应该为UsageAndDemand

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; }
}