在c#中反序列化一个返回空值的嵌套数组JSON



当我使用newtonSoft JSON反序列化JSON时,我得到空值返回。这是我的JSON:

        {"odata.metadata":"https://graph.abc.com/testapi",
    "value":
    [{"capabilityStatus":"Enabled",
    "consumedUnits":2,
    "objectId":"c9992e5ef110_6fd2c87-1e91e994b900",
    "prepaidUnits":{"enabled":25,"suspended":0,"warning":0},
    "servicePlans":
    [{"servicePlanId":"4ccb","servicePlanName":"YAMMER_ENTERPRISE"},
    {"servicePlanId":"bea4","servicePlanName":"RMS_S_ENTERPRISE"},
    {"servicePlanId":"43de","servicePlanName":"OFFICESUBSCRIPTION"},
    {"servicePlanId":"0fea","servicePlanName":"MCOSTANDARD"},
    {"servicePlanId":"e95be","servicePlanName":"SHAREPOINTWAC"},
    {"servicePlanId":"5dbe","servicePlanName":"SHAREPOINTENTERPRISE"},
    {"servicePlanId":"efb8","servicePlanName":"EXCHANGE_S_ENTERPRISE"}],
    "skuId":"42f0-b197",
    "skuPartNumber":"ENTERPRISEPACK"}]}  

,这里是我的类:

    public class PrepaidUnits
            {
            public int enabled { get; set; }
            public int suspended { get; set; }
            public int warning { get; set; }
            }
            public class ServicePlan
            {
            public string servicePlanId { get; set; }
            public string servicePlanName { get; set; }
            }
            public class Value
            {
            public string capabilityStatus { get; set; }
            public int consumedUnits { get; set; }
            public string objectId { get; set; }
            public PrepaidUnits prepaidUnits { get; set; }
            public List<ServicePlan> servicePlans { get; set; }
            public string skuId { get; set; }
            public string skuPartNumber { get; set; }
            }
            public class Graphdata
            {
            public string odata_metadata { get; set; }
            public List<Value> value { get; set; }
            }  

所以这里是问题,当我使用以下语句反序列化时,我得到servicePlanId和servicePlanName返回的空值。我哪里做错了?

        var graphAPIDataDeserialized = JsonConvert.DeserializeObject<ServicePlan>(graphResult);  

但是,当我使用Graphdata类时,我可以看到JSON响应中的所有值:

       var graphAPIDataDeserialized = JsonConvert.DeserializeObject<Graphdata>(graphResult);  

提前感谢您的帮助!

这是任何JSON解析器的预期行为。你给它的字符串应该代表你想从中得到的对象,所以当你说:

JsonConvert.DeserializeObject<ServicePlan>(graphResult)

…您指示它将图形结果反序列化为ServicePlan对象。它创建了一个ServicePlan,查看你给它的对象("图形结果"),没有找到名为servicePlanId的属性,等等,所以它没有在这些属性上设置任何值。

如果你想创建一个ServicePlan,你需要传递一个字符串值,更像{"servicePlanId":"4ccb","servicePlanName":"YAMMER_ENTERPRISE"}

另一方面,如果您试图从反序列化的图形结果中获取ServicePlans,您可以说:

JsonConvert.DeserializeObject<Graphdata>(text).value[0].servicePlans

相关内容

  • 没有找到相关文章

最新更新