JsonCpp 使用

test.json
{
    "name":"zhangsan",
    "age":25,
    "peiou":{
        "name":"lisi",
        "age":26
    }   
}
#include "json/json.h"
#include 
#include 

const std::string jsonFile("test.json");
int main() {
    std::ifstream ifs;
    ifs.open(jsonFile.c_str());
    assert(ifs.is_open());

    Json::Reader reader;
    Json::Value root;
    if (!reader.parse(ifs, root, false)) {
        return -1; 
    }   

    std::string name = root["name"].asString();
    int age = root["age"].asInt();

    Json::Value peiou = root["peiou"];
    std::string peiou_name = peiou["name"].asString();
    int peiou_age = peiou["age"].asInt();

    std::cout << name << std::endl;
    std::cout << age << std::endl;
    std::cout << peiou_name << std::endl;
    std::cout << peiou_age << std::endl;

    ifs.close();
    return 0;
}

你可能感兴趣的:(JsonCpp 使用)