反序列化 xamarin 表单中的 Json 对象数组不起作用

Deserializing a Json array of objects in xamarin forms not working

我有一个 json api 的回复:

[
{
    "id": 1,
    "accountnumber": "001303000023",
    "accounttitle": "MEGA CROWN ",
    "accountdesc": "MEGA CROWN ",
    "productType": "Loan",
    "prodname": "SME TERM LOAN                                                                                       ",
    "bookbalance": -200000.00,
    "effectivebalance": -200000.000000,
    "currentbalance": -200000.0000
},
{
    "id": 2,
    "accountnumber": "1020145429",
    "accounttitle": "MEGA CROWN",
    "accountdesc": "CORPORATE ",
    "productType": "Current",
    "prodname": "CORPORATE CURRENT ACCOUNT                                                                           ",
    "bookbalance": 3000.00,
    "effectivebalance": 23000.000000,
    "currentbalance": 3000.0000
}

]

这是我的模型 class...

    public class Balance
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("productType")]
    public string AccountType { get; set; }

    [JsonProperty("accountnumber")]
    public string AccountNumber { get; set; }
    public string accounttitle { get; set; }
    public string accountdesc { get; set; }
    public string prodname { get; set; }
    public double effectivebalance { get; set; }
    public double currentbalance { get; set; }

    [JsonProperty("currentbalance")]
    public double balance { get; set; }
    public string AccountBalance { get; set; }

    //public string AccountBalance
    //{
    //    get
    //    {
    //        string bal = this.balance.ToString();
    //        var newBal = Math.Round(Convert.ToDouble(bal), 2).ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-us")).Replace("$", "N");
    //        return newBal;
    //    }

    //    set
    //    {
    //        AccountBalance = value;
    //    }
    //}
    public ImageSource WalletImage
    {
        get
        {
            var img = ImageAsset.WalletImage;
            return img;
        }
        set
        {
            WalletImage = value;
        }

    }

    public Transaction transactions { get; set; }
}

我尝试了不同的反序列化方法,但都是徒劳的。 我尝试的第一种方法是:

                List<Balance> userAccts = JsonConvert.DeserializeObject<List<Balance>>(jsonee);

但似乎没有任何效果。每当我在上述反序列化器方法上放置断点时,调用都会到达反序列化点,但不会超出该点。老是回到之前的调用然后超时会破房子

如有任何帮助,我们将不胜感激。 注意:我什至尝试使用 stringFormatter 将“{}”添加到响应中,以便能够反序列化到列表中,但所有证明都是徒劳的。 我还尝试序列化响应然后再次反序列化它。

将反序列化方法包装到 try catch 中并检查您得到的错误是什么。

您已添加两次“currentbalance”。一个为 属性,另一个为同名的 JsonPropertyAttribute。请只保留一个。

 public double currentbalance { get; set; }

 [JsonProperty("currentbalance")]
 public double balance { get; set; }

A member with the name 'currentbalance' already exists on 'StackQA_Console1.Balance'. Use the JsonPropertyAttribute to specify another name.

保持单个“currentbalance”后,相同的代码对我有用属性。