1

I'm deserializing JSON into an object with JavaScriptSerializer in C#.

The object has the following properties:

public string plugin_name { get; set; }
public string slug { get; set; }
public string description { get; set; }
public string logo_full { get; set; }
public string[] categories { get; set; }
public Version[] versions { get; set; }

The thing is that the names (e.g. plugin_name) don't follow the usual naming guidelines (pascal case). Is there any simple way that I can give a property two identifiers? Or is there anything else that could help me achieve what I want. I'm aware that I could do this:

public string PluginName { get; set; }
public string plugin_name { set { PluginName = value; } }

But is there any simpler and cleaner solution to this?

Any help would be appreciated.

davidweitzenfeld
  • 1,021
  • 2
  • 15
  • 38
  • There are no conventions with this. It maps 1-to-1... – Simon Whitehead Apr 23 '13 at 02:20
  • possible duplicate of [JavaScriptSerializer.Deserialize - how to change field names](http://stackoverflow.com/questions/1100191/javascriptserializer-deserialize-how-to-change-field-names) – Tim S. Apr 23 '13 at 02:23

1 Answers1

2

Per this documentation, you can add an attribute to assist in this mapping instead of having to create this redirect:

[JsonProperty("plugin_name")]
public string PluginName{get;set;}

But, as pointed out, this is specific to Json.NET. Is it possible for you to use that instead?

Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
  • That attribute is for Json.net. That'd be a good alternative to recommend, but does not directly address his issue with the `JavaScriptSerializer`. http://james.newtonking.com/projects/json/help/?topic=html/JsonNetVsDotNetSerializers.htm lists the differences well, including this one ("Attribute property name customization"). – Tim S. Apr 23 '13 at 02:24
  • Thank you very much. I just tried this out and it works perfectly! – davidweitzenfeld Apr 23 '13 at 02:30