以 xamarin 形式反序列化 Json 对象数组不起作用

问题描述

我有一个类似这样的 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
}

]

这是我的模型类...

    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 将“{}”添加到响应中,以便能够反序列化为列表,但所有证据都是徒劳的。 我还尝试序列化响应,然后再次反序列化。

解决方法

将 deserialise 方法包装到 try catch 中并检查您到底得到了什么错误。

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

 public double currentbalance { get; set; }

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

名为“currentbalance”的成员已存在于 'StackQA_Console1.Balance'。使用 JsonPropertyAttribute 来指定 另一个名字。

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...