0

I'm trying to de-serialize the {"fileNumber": "1"} JSON into the following object.

CustmObj jFile = JsonConvert.DeserializeObject<CustmObj>(json)objects.

public class CustmObj
{
   [JsonProperty("fileNumber")]
   public uint FileNumber { get; set; }
}

I'm expecting it to automatically convert the int to string, however it keeps throwing the error "Error converting value .. to type 'System.UInt32'. Path ..."

How can I do this?

8
  • 1
    That's how it is supposed to work. Commented Dec 11, 2023 at 20:01
  • 2
    @AluanHaddad is correct, this is intended functionality of Json.NET. It automatically deserializes string-valued properties like "1" when the declared C# type is numeric. If you don't like that you can add a custom converter for uint like the one for int from JSON Deserialization - String Is Automatically Converted To Int. Does that question also answer yours? Commented Dec 11, 2023 at 20:07
  • 1
    You could specify a private string and mark that as the deserialization target and then use accessors to convert in your public property. But there are many ways. Commented Dec 11, 2023 at 20:08
  • 2
    Oh wait. are you saying that Json.NET will automatically convert "1" to an int but will not do so for a uint? And you are asking how to get the same behavior for uint? ... No, I can't reproduce that, int and uint behave identically. See dotnetfiddle.net/0cKL9F Commented Dec 11, 2023 at 20:10
  • 1
    Does this answer your question? JSON Deserialization - String Is Automatically Converted To Int Commented Dec 11, 2023 at 20:11

1 Answer 1

0

I'm trying to de-serialize the {"fileNumber": "1"} json into the following object.

Just remove the " of integer : {"fileNumber": 1}

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

2 Comments

Unfortunately I can't, the string is only a part of a larger file which is not generated by myself.
so, follow the link post above : stackoverflow.com/questions/41783225/…

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.