Search code examples
jsonxamarinxamarin.formsjson-deserialization

Deserializing a Json array of objects in xamarin forms not working


I have a json api response of something like this:

[
{
    "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
}

]

and here is my model 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; }
}

I have tried different approaches to deserialize but all is futile. first method I tried is this:

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

But nothing seem to work. whenever I put a breakpoint on the above deserializer method, the call gets to the point of deserialization but doesn't go beyond that. It's always returning to the previous call then overtime will break the house.

Please any help will be deeply appreciated. Note: I have even tried to add "{}" into the response using stringFormatter so as to be able to deserialize into a list but all proof futile. I also tried to serialize the response then deserialize it again.


Solution

  • Wrap deserialise method into try catch and check what error you get exactly.

    You have added "currentbalance" twice. One as property and other one as JsonPropertyAttribute with same name. Please keep only one.

     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.

    Same code works for me after keeping single "currentbalance" property.