Access property of dynamic object when property name is a keyword

Author : Sachin Sharma
Published On : 25 Aug 2023

I am trying to access a property of a JSON object like

dynamic myJsonData = JObject.Parse("{ \"out\":123, \"xyz\": 456 }");
Console.WriteLine(myJsonData.xyz); 
Console.WriteLine(myJsonData.out);  here compile time error because out is keyword

So now question is how to remove that error ?

With the use of @ operator can remove error.

dynamic myJsonData = JObject.Parse("{ \"out\":123, \"xyz\": 456 }");
Console.WriteLine(myJsonData.@out); 

Output : 123

I hope this solution will help you to remove that kind of error.

 

Comments

No comments have been added to this article.

Add Comment