【问题标题】:What is the best way to get access to nested JSON in D?在 D 中访问嵌套 JSON 的最佳方法是什么?
【发布时间】:2016-05-09 09:56:29
【问题描述】:

现在我正在使用 vibed JSON 模块,但我不知道如何在不使用 foreach 迭代的情况下访问嵌套元素。

这是我的 JSON:

{
    "hasMore": false,
    "result": [{
        "ip": "127.0.0.1",
        "passedtests": "[firsttest8,firsttest8,firsttest8,firsttest8]",
        "guid": ""
    }],
    "code": 201,
    "extra": {
        "stats": {
            "writesIgnored": 0,
            "scannedIndex": 0,
            "scannedFull": 1,
            "executionTime": 0,
            "filtered": 0,
            "writesExecuted": 0
        },
        "warnings": []
    },
    "error": false,
    "cached": false
}

我想做类似的事情:result.passedtests。但是这里的结果是一个数组。

【问题讨论】:

  • 我不知道如何在不使用 foreach 迭代的情况下访问嵌套元素。 为什么 foreach 有问题?

标签: json d vibed


【解决方案1】:

我找到了完美的解决方案:

Json resultPassedtestsJson = visitorsInfo["result"][0]["passedtests"]; // "[firsttest8,firsttest8,firsttest8,firsttest8]"

[0] 是因为result 是数组,我们访问它的第一个元素。

【讨论】:

  • 为什么误用答案?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-16
  • 1970-01-01
  • 2016-12-17
  • 2010-10-12
  • 1970-01-01
相关资源
最近更新 更多