khukho
khukho

Reputation: 476

How to parse decimal in C#

I'm using C#, UWP and Newtonsoft.Json library to parse text to json.

The data I'm trying to parse is:

[
    {
        "id": 9,
        "displayNo": 1,
        "ygnGoldPrice": 1111111.00,
        "worldGoldPrice": 33333.00,
        "usDollars": 1640.00,
        "differenceGoldPrice": 12000.00,
        "updatedDate": "2021-04-23T14:59:11Z"
    },
    {
        "id": 10,
        "displayNo": 2,
        "ygnGoldPrice": 12345.00,
        "worldGoldPrice": 1222.00,
        "usDollars": null,
        "differenceGoldPrice": 1222.00,
        "updatedDate": "2021-04-23T15:01:23Z"
    }
]

As you can see, some of the fields have decimal point .00 and I'm trying to parse them with the following model.

class GoldPrice
{
    public int id { get; set; }
    public int displayNo { get; set; }
    public decimal ygnGoldPrice { get; set; }
    public decimal worldGoldPrice { get; set; }
    public decimal usDollars { get; set; }
    public decimal differenceGoldPrice { get; set; }
    public string updatedDate { get; set; }
}

The method that parses

public async Task<ObservableCollection<T>> GetAll<T>(ObservableCollection<T> list, string path = "")
{
    HttpClient httpClient = new HttpClient();
    Uri requestUri = new Uri(baseUri + path);

    HttpResponseMessage httpResponse = new HttpResponseMessage();

    string httpResponseBody;
    try
    {
         httpResponse = await httpClient.GetAsync(requestUri);
         httpResponse.EnsureSuccessStatusCode();
         httpResponseBody = await httpResponse.Content.ReadAsStringAsync();

         Debug.WriteLine(httpResponseBody); // The data is printed out. OK.

         return JsonConvert.DeserializeObject<ObservableCollection<T>>(httpResponseBody);
     }
     catch (Exception ex)
     {
         httpResponseBody = "Error: " + ex.HResult.ToString("X") + " Message: " + ex.Message;
         return null;
     }
 }

I'm getting the following error in the line return JsonConvert.DeserializeObject... saying that Exception thrown: 'Newtonsoft.Json.JsonSerializationException' in Newtonsoft.Json.dll.

Upvotes: 1

Views: 635

Answers (2)

Hasta Tamang
Hasta Tamang

Reputation: 2263

Other alternatives:

  1. Use property attribute

     [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
     public decimal usDollars { get; set; }
    
  2. Specifiy null value handling options for the JsonSerializer

    var serializerSettings = new JsonSerializerSettings
    {
           NullValueHandling = NullValueHandling.Ignore
    };
    
    List<GoldPrice> goldPrices = JsonConvert.DeserializeObject<List<GoldPrice>>(json, serializerSettings);
    

Upvotes: 2

khukho
khukho

Reputation: 476

The problem is that the data I'm trying to parse has null, as you can see in the question.

Therefore, I have to change my model like below, which is capable of accepting null and it works.

class GoldPrice
{
    public int id { get; set; }
    public int displayNo { get; set; }
    public decimal? ygnGoldPrice { get; set; }
    public decimal? worldGoldPrice { get; set; }
    public decimal? usDollars { get; set; }
    public decimal? differenceGoldPrice { get; set; }
    public string updatedDate { get; set; }
}

Upvotes: 1

Related Questions