Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有一个未知的 JSON,我想检查它是否有类似这样的形式:
{ "foo": stuff "bar": stuff }
任何东西在哪里stuff——整数、对象等等。如果我做这样的事情:
stuff
auto json = parseJSON("{}"); auto foo = json["foo"];
我会得到一个段错误。有什么方法可以优雅地处理这个问题(返回 null、抛出异常、除了段错误之外的任何东西)?
只需使用 Din运算符,就像使用 D 关联数组:
in
auto foo = "foo" in json ? json["foo"].str : null;
如果您使用的是 DMD 2.065 或更早版本,则需要使用运算json.object符in:
json.object
auto foo = "foo" in json.object ? json["foo"].str : null;