Search code examples
c#.netrestrestsharp

RestSharp - GET request with "$" response attribute name


I need to get GET response from REST API. I use RestSharp. The problem is, that one name of the response attribute is "$". This is the response:

[
    {
        "CodeId": {
            "$": "00000000"
        },
        "Entity": {
            "LegalName": {
                "@xml:lang": "cs",
                "$": "xxxxx"
            }
        }
    }
]

How should I use the RestSharp to get the value of Entity.LegalName.$ ?


Solution

  • I found the answer thanks by @fredrik.

          var client = new RestClient(url);
    
          var request = new RestRequest(urlRequest, DataFormat.Json);
    
          var response = client.Get(request);
    
          Console.WriteLine(JsonSerializer.Deserialize<List<TestRestResponseTemplate>>(response.Content)[0].Entity.LegalName.Value);
    

    TestRestResponseTemplate:

      public class TestRestResponseTemplate
      {
        public Entity Entity { get; set; }
      }
    
      public class LegalName
      {
        [JsonPropertyName("@xml:lang")]
        public string Language { get; set; }
    
        [JsonPropertyName("$")]
        public string Value { get; set; }
      }
    
      public class Entity
      {
        public LegalName LegalName { get; set; }
      }