I am using the JQuery QueryBuilder plugin on my site, which compiles it's data into JSON. On the server side code, I take the JSON that is returned from the plugin and parse it into a dynamic variable. The problem is, one of the keys is "operator", and the word operator is a c# keyword. I've tried using _operator, __operator, but these do not work. Any idea how I can access the value of the key "operator"?
For example, here is the JSON that I am working with (simplified, of course):
[{
"id":"ABC",
"value":"test",
"operator":"equal"
}]
And here is the server side C# code
dynamic json = JObject.Parse(model.pJson);
foreach (var item in json.rules) {
string id = item.id;
string value = item.value;
string _operator = item.operator; // HERE IS THE PROBLEM! I cannot type item.operator because operator is a keyword
}
You can use @
to use a keyword as an identifier, so this should work:
string _operator = item.@operator;
Alternatively, you may well find that indexer access would be fine:
string _operator = item["operator"];
See more on this question at Stackoverflow