Anders Martini
Anders Martini

Reputation: 948

parse Json with key "params" in mvc4

I am receiving a json-string though a HTTP-POST from an external service (which is out of my control). One of the keys in this json is params:{...},

i need to map this json to a class MyClass. This would be simple, were it not for the fact that params is a keyword in C#..

I've tried using Newtonsofts json.NET(version 7.0.0) library, and have the following in MyClass:

    [JsonProperty(PropertyName = "params")]
    public TrustlyNotifParams Parameters { get; set;}

but Parameters ends up as null. The posted json has a nested value for params, I've seen this through logging.

everything above Parameters in the hierarchy gets parsed just fine. What am I doing wrong here?

Upvotes: 0

Views: 161

Answers (1)

Ron Beyer
Ron Beyer

Reputation: 11273

You can (sparingly please) make keywords as the names of properties or variables, you just need to tell the compiler to treat it as such. This is the same way you tell it to treat a string as literal instead of using escape sequences:

public TrustlyNotifParams @params { get; set; }

The @ symbol tells the compiler to treat the following as a variable, field, property name, etc.

Upvotes: 1

Related Questions