Question

I get proper result from REST Web Api, But when I try to Deserialize the result, It's change the string value "034342323" to "7455955"

My Class

public class DataModel
     {

         public int Id { get; set; }

         public string ABN { get; set; }

         public string AccountNumber { get; set; }

     }

JSON Result

[{"Id":1,"ABN":"9949876532","AccountNumber":"034342323"}]

Calling code

 using (HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        var result = await response.Content.ReadAsStringAsync();

                        result = result.Replace("\"", string.Empty).Trim();
                        result = result.Replace("\\", string.Empty).Trim();

                        var data = JsonConvert.DeserializeObject<IEnumerable<DataModel>>(result);

                      [...]


                    }

All the value remain same accept the AccountNumber change automatically

enter image description here

Note: We found if any string starting with "0" , during de-serialization it is change autometically !!!!

Now it is become

[{"Id":1,"ABN":"9949876532","AccountNumber":"7455955"}]

Additional

If I don't remove "" it is unable to Desirialize & throws error

{"Error converting value \"[{\"Id\":1,\"ABN\":\"9949876532\",\"AccountNumber\":\"034342323\"}]\" to type 'System.Collections.Generic.IEnumerable`1[DataModel]'
  . Path '', line 1, position 174."}

        Could not cast or convert from System.String to System.Collections.Generic.IEnumerable`1[DataModel].

Solution

   result = result.Replace("\"", "").Trim();
   result = result.Replace("\\", "'").Trim(); //This line is the tricks

Thanks To Andrew for his explanation. So I overcome this issue, by minor modification.

Was it helpful?

Solution

This behavior is by design: integer strings leading with "0" will be treated as octal numbers(octal 34342323 == decimal 7455955) by Json.Net:

http://json.codeplex.com/workitem/22097

When you generate your json, please review the code that adds AccountNumber, make sure you remove leading "0".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top