0

Question Background:

I am calling an API that I have no control over. It is return a JSON response which contains property names with symbols such as '@'.

The Issue:

This is an example of the JSON returned:

{"@currencyId":"USD", "__value__":"7.85"}

I am converting this to a C# POCO such as:

public Class Obj
{
   public string @currencyId {get; set;}
   public string __value__ {get; set;}
}

Even before I am able to use any library such as JSON.NET this is causing errors.

What is the best method to convert the response to property names that can be used with my C# POCO?

user1352057
  • 3,162
  • 9
  • 51
  • 117
  • 1
    Looks like a duplicate of [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/q/24536533). [This answer](https://stackoverflow.com/a/24536739) is the one that applies in your case, since you have a fixed schema. See also [How to deserialize a property with a dash (“-”) in it's name with NewtonSoft JsonConvert?](https://stackoverflow.com/q/14753113) which includes an answer for `DataContractJsonSerializer`. But the specific answer will depend on the serializer you are using. Is it Json.NET? – dbc Nov 02 '17 at 19:41
  • Note however that if you are using `JavaScriptSerializer` that remapping of property names via attributes is not supported. see [JavaScriptSerializer - custom property name](https://stackoverflow.com/q/32487483/3744182) for details. – dbc Nov 02 '17 at 21:19

0 Answers0