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#?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With