【问题标题】:Null Object-DeSerializing JSON C#空对象反序列化 JSON C#
【发布时间】:2022-01-18 12:38:16
【问题描述】:

返回对象:

{
    "_expands": [],
    "size": 3,
    "start": 3,
    "limit": 3,
    "isLastPage": false,
    "_links": {
        "base": "http://host:port/context/rest/desk",
        "context": "context",
        "next": "http://host:port/context/rest/desk",
        "prev": "http://host:port/context/rest/desk"
    },
    "values": [
        {
            "status": "Waiting for Customer",
            "statusDate": {
                "iso8601": "2015-10-08T14:05:00+0700",
                "polaris": "2015-10-08T14:05:00.000+0700",
                "friendly": "Today 14:05 PM",
                "epochMillis": 1444287900000
            }
        },
        {
            "status": "Waiting for Support",
            "statusDate": {
                "iso8601": "2015-10-08T14:01:00+0700",
                "polaris": "2015-10-08T14:01:00.000+0700",
                "friendly": "Today 14:01 PM",
                "epochMillis": 1444287660000
            }
        },
        {
            "status": "Waiting for Customer",
            "statusDate": {
                "iso8601": "2015-10-08T14:00:00+0700",
                "polaris": "2015-10-08T14:00:00.000+0700",
                "friendly": "Today 14:00 PM",
                "epochMillis": 1444287600000
            }
        }
    ]
}

类:

 public class polarisState
{
    public string[] expands { get; set; }
    public int size { get; set; }
    public int start { get; set; }
    public int limit { get; set; }
    public bool isLastPage { get; set; }
    public _links links { get; set; }
    public values[] values { get; set; }
}




public class _links
{

    //public string base {get; set;}
    public string context { get; set; }
    public string next { get; set; }
    public string prev { get; set; }
}

public class values
{

    public string status { get; set; }
    public statusDate statusDate { get; set; }

}

public class statusDate
{

    public string iso8601 { get; set; }
    public string polaris { get; set; }
    public string friendly { get; set; }
    public int epochMillis { get; set; }

}

代码如下:

 if (resp2.IsSuccessStatusCode)
    {
       

        var values = JsonConvert.DeserializeObject<JiraState>(resp2.Content.ReadAsStringAsync().Result); }

【问题讨论】:

  • 您的问题/问题是什么,您尝试过什么?
  • 问题是我在反序列化 Web 服务返回的对象时得到一个空对象

标签: c# json rest web-services deserialization


【解决方案1】:

您应该将 PolarisState 类更改为

public class polarisState
{
    public List<string> expands { get; set; }
    public int size { get; set; }
    public int start { get; set; }
    public int limit { get; set; }
    public bool isLastPage { get; set; }
    public _links links { get; set; }
    public List<values> values { get; set; }
}

数组到列表。 然后将 statusDate 更改为:

public class statusDate
{

    public string iso8601 { get; set; }
    public string polaris { get; set; }
    public string friendly { get; set; }
    public long epochMillis { get; set; }

}

将 epochMillis 从 int 更改为 long 值类型。 完成了。

使用异步方法,修改代码如下:

var content = await resp2.Content.ReadAsStringAsync();
var values = JsonConvert.DeserializeObject<polarisState>(content);

【讨论】:

  • 调试时我可以看到返回的对象,一旦反序列化它就是一个空对象
  • 如果在statusDate类中将epochMillis的数组改为List,int改为long,效果很好。
  • 刚刚验证了返回的对象(调试),缺少一些属性。它现在正在工作。
猜你喜欢
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-28
相关资源
最近更新 更多