为什么存在数据时反序列化的对象字段会变为null?

时间:2019-03-13 17:22:52

标签: c# arrays json serialization json.net

我有一个需要反序列化的json对象,我正在使用 Json.NET 进行这些操作。

当它是一个简单的对象时,它很容易做到,但我不知道如何反序列化此字符串

json

{
    "aspsp-list":
    [
        {
            "id":"424250495054504C",
            "bic":"BBPIPTPL",
            "bank-code":"0010",
            "aspsp-cde":"BBPI",
            "name":"BANCO BPI, SA",
            "logoLocation":"../img/corporate/theBank.jpg",
            "api-list":[{
                "consents":["BBPI/v1/consents"],
                "payments":["BBPI/v1/payments"],
                "accounts":["BBPI/v1/accounts"],
                "funds-confirmations":["BBPI/v1/funds-confirmations"]
            }]
        },
        {
            "id":"544F54415054504C",
            "bic":"TOTAPTPL",
            "bank-code":"0018",
            "aspsp-cde":"BST",
            "name":"BANCO SANTANDER TOTTA, SA",
            "logoLocation":"../img/openBank.svc",
            "api-list":[{
                "consents":["BBPI/v1/consents"],
                "payments":["BBPI/v1/payments"],
                "accounts":["BBPI/v1/accounts"],
                "funds-confirmations":["BST/v1/funds-confirmations"]
            }]
        }
    ]
}

现在我拥有的代码是

internal class AspspListResponseResource 
{
    // Report with the list of supported ASPSPs. Each ASPSP will include the list of available API endpoints and the logo.
    [JsonProperty(PropertyName = "aspsp-list")]
    public AspspList[] AspspList { get; set; }

    public AspspListResponseResource() { /* Empty constructor to create the object */ }

     public AspspListResponseResource(string jsonString)
     {
        //var alrr = JsonConvert.DeserializeObject<AspspListResponseResource>(jsonString);

        JObject jObject = JObject.Parse(jsonString);
        JToken jUser = jObject["aspsp-list"];

        // The root object here is coming with certain fields as null, such as 'aspsp-cde', 'bank-code' and 'api-list'
        AspspListResponseResource root = JsonConvert.DeserializeObject<AspspListResponseResource>(jsonString);                        
     }
}

internal class Aspsp
{
    // ASPSP Id
    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; } = "";

    // Bank Identifier Code
    [JsonProperty(PropertyName = "bic")]
    public string Bic { get; set; } = "";

    // IBAN Bank Identifier
    [JsonProperty(PropertyName = "bank-code")]
    public string BankCode { get; set; } = "";

    // ASPSP Code to use in the endpoint
    [JsonProperty(PropertyName = "aspsp-cde")]
    public string AspspCde { get; set; } = "";

    // Institution name
    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; } = "";

    // Bank logo location
    [JsonProperty(PropertyName = "logoLocation")]
    public string LogoLocation { get; set; } = "";

    // Bank Supported API List
    [JsonProperty(PropertyName = "api-list")]
    public ApiLink[] ApiList { get; set; }
}

internal class ApiLink
{
    // Consents Link List
    [JsonProperty(PropertyName = "consents")]
    public string[] Consents { get; set; } = { "" };

    // Payments Link List
    [JsonProperty(PropertyName = "payments")]
    public string[] Payments { get; set; } = { "" };

    // Accounts Link List
    [JsonProperty(PropertyName = "accounts")]
    public string[] Accounts { get; set; } = { "" };

    // Balances Link List
    [JsonProperty(PropertyName = "balances")]
    public string[] Balances { get; set; } = { "" };

    // Transaction Link List
    [JsonProperty(PropertyName = "transaction")]
    public string[] Transaction { get; set; } = { "" };

    // Funds-Confirmations Link List
    [JsonProperty(PropertyName = "funds-confirmations")]
    public string[] FundsConfirmations { get; set; } = { "" };

}

即使jsonString确实具有数据,反序列化对象的值之和也为null。

我应该如何进行?

3 个答案:

答案 0 :(得分:2)

您的json的结构方式:

{
 "aspsp-list":
    [
        {
            "id":"123123123",
            "bic":"BBPIPTPL",
            "bank-code":"0010",
            "aspsp-cde":"BBPI",
            "name":"BANCO BPI, SA",
            "logoLocation":"../img/corporate/theBank.jpg",
            "api-list":[{
                "consents":"",
                "payments":"",
                "accounts":"",
                "funds-confirmations":""
            }]
        },
        {
            "id":"1434231231",
            "bic":"TOTAPTPL",
            "bank-code":"0018",
            "aspsp-cde":"BST",
            "name":"BANCO SANTANDER TOTTA, SA",
            "logoLocation":"../img/openBank.svc",
            "api-list":[{
                "consents":"",
                "payments":"",
                "accounts":"",
                "funds-confirmations":""
            }]
        }
    ]
}

这告诉我们您有一个对象,其中包含一个称为Aspsp-list的对象数组。

如果这是您的理想选择。 我们需要创建一个与此类似的对象

public class RootJsonObject {
    public IEnumerable<Aspsp> Aspsp-list {get; set;}
}

简单地反序列化: JsonConvert.Deserialize<RootJsonObject>(/*your json string*/ value);

如果只想使用数组,则需要将反序列化纯为IEnumerable/Array,但是还需要将json更改为只是数组,而不是包装数组的对象。

答案 1 :(得分:0)

您需要为ApiLink类创建一个构造器,以便执行以下操作:

var apiListRaw = new ApiLink(value["api-list"][0] as JObject);

构造函数如下所示:

public ApiLink(JObject json)
{
    Consensts = (string[])json["consents"];
    ...
}

答案 2 :(得分:0)

我现在设法使其工作了,我的问题不完全是因为数据类型或结构(至少不是完全不完整,请说结构错误是部分正确的评论)而无法进行反序列化。

所以,这就是我解决问题的方式:

->在AspspListResponseResource类上创建了一个空的构造函数,以便方法JsonConvert.DeserializeObject<T>(jsonString)可以创建对象的实例,我想到了这一点,因为唯一的构造函数使用了string ,因此没有其他构造器可供JsonConvert使用。

->在[JsonProperty(PropertyName = "")]的帮助下放入的字段名称,但这仍然给我反序列化的objectnull或带有一些空字段的信息。

->注释了Transaction类的字段FundsConfirmationsApiLink,这些字段在Web API的文档中,因此我将它们放入其中,但查看了json字符串我收到的消息是,它们似乎没有被使用,所以我只发表了评论

在完成这些更改之后,代码现在可以正常工作了:

代码:

internal class AspspListResponseResource 
{
    // Report with the list of supported ASPSPs. Each ASPSP will include the list of available API endpoints and the logo.
    [JsonProperty(PropertyName = "aspsp-list")]
    public Aspsp[] AspspList { get; set; }

    public AspspListResponseResource() { /* Empty constructor to create the object */ }

     public AspspListResponseResource(string jsonString)
     {
        AspspListResponseResource root = JsonConvert.DeserializeObject<AspspListResponseResource>(jsonString);
        this.AspspList = root.AspspList;
     }
}

internal class Aspsp 
{
    // ASPSP Id
    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; } = "";

    // Bank Identifier Code
    [JsonProperty(PropertyName = "bic")]
    public string Bic { get; set; } = "";

    // IBAN Bank Identifier
    [JsonProperty(PropertyName = "bank-code")]
    public string BankCode { get; set; } = "";

    // ASPSP Code to use in the endpoint
    [JsonProperty(PropertyName = "aspsp-cde")]
    public string AspspCde { get; set; } = "";

    // Institution name
    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; } = "";

    // Bank logo location
    [JsonProperty(PropertyName = "logoLocation")]
    public string LogoLocation { get; set; } = "";

    // Bank Supported API List
    [JsonProperty(PropertyName = "api-list")]
    public ApiLink[] ApiList { get; set; }
}

internal class ApiLink 
{
    // Consents Link List
    [JsonProperty(PropertyName = "consents")]
    public string[] Consents { get; set; } = { "" };

    // Payments Link List
    [JsonProperty(PropertyName = "payments")]
    public string[] Payments { get; set; } = { "" };

    // Accounts Link List
    [JsonProperty(PropertyName = "accounts")]
    public string[] Accounts { get; set; } = { "" };

    // Balances Link List
    [JsonProperty(PropertyName = "balances")]
    public string[] Balances { get; set; } = { "" };

    //// Transaction Link List
    //[JsonProperty(PropertyName = "transaction")]
    //public string[] Transaction { get; set; } = { "" };
    //
    //// Funds-Confirmations Link List
    //[JsonProperty(PropertyName = "funds-confirmations")]
    //public string[] FundsConfirmations { get; set; } = { "" };

}