-1

I have below json object to convert for c# class.

{
   "Token": “Token”,
   "ref": 1 
}

The converted c# class is given below.

public class ABC
{
public string Token{get;set;}
public int ref{get;set;}
}

But I received following error message from c#.

Member modifier 'ref' must precede the member type and name

How do I convert JSON into c# class correctly with ref attribute?

Thanks in advance.

wiki
  • 165
  • 1
  • 1
  • 6

1 Answers1

2

In Newtonsoft.Json, you could do this:

public class ABC
{
    public string Token{get;set;}
    [JsonProperty("ref")]
    public int Ref{get;set;}
}

If you don't want to do that or can't do that, use this:

public class ABC
{
    public string Token { get; set; }
    public int @ref {get; set; }
}

Since ref is a C# keyword, you can add the @ symbol which allows you to use it as a property name.

var thing = new ABC { @ref = 0 };
thing.@ref = 5;

Your models will serialize as if it were defined as ref.

Andy
  • 12,859
  • 5
  • 41
  • 56