如何遍历字符串并获取单个值jsoncpp

时间:2015-03-09 05:39:22

标签: jsoncpp

输入json字符串为[{“description”:“sample”“,”id“:3,”name“:”xyz“,”numOfSnapshots“:132,”storagePool“:1,”volSize“:32 },]

如何提取列表并获取密钥和值

1 个答案:

答案 0 :(得分:0)

std::vector<std::string> keys = json.getMemberNames();
for (std::vector<std::string>::const_iterator it = keys.begin(); it != keys.end(); ++it)
{
    if (json[*it].isString())
    {
        std::string value = json[*it].asString();
    }
    else if (json[*it].isInt())
    {
         int value = json[*it].asInt();
    }
    // and so on for each type...
}

...如果你有boost,你可以用非常优雅的方式替换循环:

BOOST_FOREACH(const std::string &key, json.getMemberNames())