1

I need to parse JSON file in C# code by using JSON.net (Newtonsoft)

But json file I receive begins as this:

{"3h":3}

the variable name begins with number but c# can't do like this.

How can I set the value in the right way? Should I swap the variable name by my self? That would make very dirty code.

Thank you.

tjPark
  • 308
  • 1
  • 11

1 Answers1

2

You can do this little focus with mapping:

class Program
{
    static void Main(string[] args)
    {
        string jsonInput = @"{""3h"":3}";
        var result = (myJsonObj)JsonConvert.DeserializeObject<myJsonObj>(jsonInput);
        Console.WriteLine(result.MyProperty);

    }
}

public class myJsonObj
{
    [JsonProperty(PropertyName = "3h")]
    public string MyProperty { get; set; }
}
Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46