使用Boost遍历JSON中的对象数组

时间:2013-06-03 13:41:50

标签: json boost

我自己无法解决这个问题,也找不到类似的问题。我有这个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": {
        }
    }
}
}

我在试图恢复两种不同真菌物种的信息时陷入困境。我能够提取简单的参数,如depth,initialPatches等......但是如何遍历所有真菌物种却完全迷失了...任何提示?

提前致谢,

1 个答案:

答案 0 :(得分:3)

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

  1. property_tree
  2. JSON parser使用上面的
  3. example使用JSON解析器
  4. 更新

    试试这个:

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