Search code examples
c#jsonbson

How to parse a $ (dollar) containing field using c#?


Could any one please let me know that how can we parse a field that starts with $?

see the sample below,

{
    id: 123,
    $firstName: "abc",
    $lastName: "xyz"
}

I used to parse it through below object but getting the value empty.

var jsonString = "{\"id\": 123,\"$firstName\": \"abc\", \"$lastName\": \"xyz\"}";
var restaurant = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.Data>(jsonString);

Data

public class Data
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

FirstName and LastName are getting null

Please advice what I'm doing wrong.


Solution

  • You can try to use [JsonProperty]

    [JsonProperty(PropertyName = "$firstName")]
    public string firstName{ get; set; }