且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

.NET 4中:如何引用名为QUOT性和动态对象;回归"

更新时间:2023-01-17 16:22:15

尝试响应。@ return.high.currency

您需要追加 @ 在任何领域至极的名字开始时是一样的C#的关键字。

I'm retrieving json from a public api and converting it into a dynamic object using JsonFx.

JsonFx.Json.JsonReader reader = new JsonFx.Json.JsonReader();
dynamic response = reader.Read(jsonAsString);

The json contains a property named return. e.g.

{"result":"success","return":{"high":{"value":"3.85001","value_int":"385001","display":"3.85001\u00a0\u20ac","currency":"EUR"}}

JsonFx creates the dynamic object fine and I can also debug into it and see the values. The problem is when I try to reference the property in my code the compiler complains:

response.return.high.currency
Identifier expected; 'return' is a keyword  

How can I reference the return property without the compiler complaining?

Try response.@return.high.currency.

You need to append @ at the begining of any field wich name is the same as C# keywords.