Reputation: 11833
I am trying to access a property of a JSON object like this:
using Newtonsoft.Json.Linq;
dynamic myJsonData = JObject.Parse("{ \"out\":123, \"xyz\": 456 }");
Console.WriteLine(myJsonData.xyz); //works
Console.WriteLine(myJsonData.out); //compiler error ";" expected
However, the last line does not compile.
Is there a simple way to use the dynamic property to get the value of "out" even though out is a keyword in C#?
Upvotes: 4
Views: 789
Reputation: 24916
It should be solved by adding @
before the reserved keyword:
Console.WriteLine(myJsonData.@out);
Here is a quote from MSDN:
Keywords are predefined, reserved identifiers that have special meanings to the compiler. They cannot be used as identifiers in your program unless they include @ as a prefix. For example, @if is a valid identifier but if is not because if is a keyword.
Upvotes: 8