【问题标题】:std.json - A bit confused with TRUE, FALSE and NULL valuesstd.json - 与 TRUE、FALSE 和 NULL 值有点混淆
【发布时间】:2014-11-18 00:57:47
【问题描述】:

我正在查看 std.json 库作为我正在处理的程序的一部分,我有点困惑如何从 JSONValues 中获取数据,其类型被推断为 TRUE、@987654324 @ 或NULL

例如,如果我解析以下 JSON:

{
    "foo" : "bar"
}

然后我可以通过执行以下操作来提取属性 "foo" 中保存的字符串:

auto json = parseJSON("/path/to/json/example.json");
auto foo_attr = json["foo"].str;

但假设我有这样的 JSON:

{
    "foo" : false,
    "bar" : true,
    "baz" : null
}

我需要做什么才能获得"foo""bar""baz" 的属性值?

【问题讨论】:

    标签: json d phobos


    【解决方案1】:

    查看变量的类型。

    auto json = parseJSON("/path/to/json/example.json");
    bool foo = json["foo"].type == JSON_TYPE.TRUE;
    bool bar = json["bar"].type == JSON_TYPE.TRUE;
    bool bazIsNull = json["baz"].type == JSON_TYPE.NULL;
    

    当然,如果您预计值可能有其他类型,则需要额外检查。

    【讨论】:

      猜你喜欢
      • 2016-03-27
      • 2015-02-21
      • 2011-11-18
      • 1970-01-01
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 2015-02-22
      相关资源
      最近更新 更多