json - std.json - 与 TRUE、FALSE 和 NULL 值有点混淆

标签 json d phobos

我正在查看 std.json 库,作为我正在开发的程序的一部分,并且我对如何从 JSONValue 中获取数据感到有点困惑其类型被推断为 TRUEFALSENULL

例如,如果我解析以下 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" 的属性值?

最佳答案

查看变量的类型。

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;

当然,如果您期望值可能具有其他类型,则需要额外的检查。

关于json - std.json - 与 TRUE、FALSE 和 NULL 值有点混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26984818/

相关文章:

java - PHP DateTime到Java Date

javascript - angularJS - 按键过滤 JSON

python - Pandas:使用 read_json 读取大 bz2 文件的前 x block 或行

d - 有D的包裹管理员吗?

d - std.algorithm.joiner(string[],string) - 为什么结果元素是 dchar 而不是 char?

iOS : display svg from JSON

windows - 为导致崩溃的非静态函数指针赋值 - 为什么?

compiler-construction - 使用 dmd 2.063 构建项目期间临时生成的符号数量是否有限制?

d - std.algorithm.find 应该要求对范围元素的引用吗?

d - 如何在 D 中使用 tolower