0

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.

4
  • your model does not match your json - there is no "id" or "accountnumber", etc in the json. Use json2csharp.com to help generate a matching model. Commented Mar 10, 2021 at 12:47
  • I have editted my the json response to how it is exactly but its not working. you can check my edits above Commented Mar 10, 2021 at 14:14
  • dotnetfiddle.net/5VR5rs Commented Mar 10, 2021 at 14:21
  • Wrap deserialise method into try catch and check what error you get in the catch exactly Commented Mar 10, 2021 at 15:50

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Ranjit. I realize the error is because of json property conflict.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.