-1

I faced a problem that I could not find any solution to by google...

   JavaScriptSerializer js = new JavaScriptSerializer();
   MyClass myObject = js.Deserialize<MyClass>(jsonstring); 

What about if an attribute in the json data is called "short"? I cannot make a class like this:

public class MyClass
{
   public int A;        
   public int B;       
   public int short;
}

So how can I get the jsonstring into an object in an easy way? Very thankful for all help I can get.

Tommy
  • 1
  • Does this answer your question? [JavaScriptSerializer - custom property name](https://stackoverflow.com/questions/32487483/javascriptserializer-custom-property-name) and specifically [that](https://stackoverflow.com/a/32488106/1797425) answer... – Trevor Nov 04 '21 at 19:35

2 Answers2

1

You can use @ in

public class MyClass
{
   public int A;        
   public int B;       
   public int @short;
}
Balastrong
  • 4,336
  • 2
  • 12
  • 31
1

short is a keyword (documentation; you will need to call the variable something else. Short would work just fine since keywords are case sensitive, and all keywords are lowercase.

In @Balastrong's answer:

public class MyClass
{
   public int A;        
   public int B;       
   public int @short;
}

The @ symbol is being used as a "verbatim identifier" (documentation; see number 1). This allows the interpreter to understand @short as the identifier short instead of the keyword short.

Hope this cleared some things up.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Zach W
  • 194
  • 11