使用Boost从JSON遍历对象数组



我没有自己解决这个问题,也没有发现类似的问题。我有这个JSON:

{
"world": {
    "soil": {
        "dimensions": {
            "depth": "200",
            "length": "200",
            "width": "200",
            "cellSize": "1"
        },
        "moisture": {
            "min": "0",
            "max": "100",
            "initialPatches": "30",
            "initialPatchesMaxWidth": "100",
            "initialPatchesSigma": "3",
            "initialPatchesUseRandom": "true"
        },
        "nutrients": {
            "minC": "0",
            "maxC": "23",
            "minN": "0",
            "maxN": "23",
            "minP": "0",
            "maxP": "23"
        },
        "temperature": "25"
    },
    "life": {
        "fungi": [
            {
                "name": "fungus gungus",
                "sporeMass": "0.02"
            },
            {
                "name": "fungus gungusim",
                "sporeMass": "0.04"
            }
        ],
        "fungivores": {
        },
        "predators": {
        }
    }
}
}

我在试图恢复两种不同真菌物种的信息时陷入了僵局。我能够提取简单的参数,如深度,initialPatches等…但是完全迷失在如何遍历所有真菌物种…有提示吗?

提前感谢,

何塞

如果你用谷歌搜索它,你可以找到几个结果:

  1. property_tree
  2. JSON解析器使用以上
  3. 使用JSON解析器的示例

更新

Try this:

BOOST_FOREACH(const ptree::value_type& child, node.get_child("life.fungi")) {
    std::cout
        << child.second.get<std::string>("sporeMass")
        << "n";
}

最新更新