1

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.

1 Answer 1

2

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".

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

10 Comments

But it starts interpreting like this only after he removes quotes. As long as it string(in quotes) it should not matter, right?
it does. please have a look at section 3 here samuli.hakoniemi.net/…
This sounds like the behavior witnessed, but it does not look by design at all - it looks like a bug. The work item you linked to talks about numbers where in OP's case they are deserializing a string.
@BenjaminGruenbaum, OP removes quotes in his code, meaning "AccountNumber":"034342323" becomes AccountNumber:034342323, and json.net starts treating the number as octal
@ Andrew: If I need 0 is their any way do that ?
|

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.