首页 > 代码库 > jsoncpp的使用

jsoncpp的使用

开源代码地址 github:https://github.com/open-source-parsers/jsoncpp

使用的时候直接包含.h和.cpp到项目中,也可以编译成lib库来使用。

json简单字符串解析:

 1 //简单Json
 2 void Config1()
 3 {
 4     Json::Value root;
 5     Json::Reader reader;
 6     std::string str = "{\"ip\":\"127.0.0.1\",\"port\":\"8080\"}";
 7     //
 8     if (!reader.parse(str, root))
 9     {
10         //error
11     }
12     std::ofstream out;
13     out.open("config1.json", std::ios::binary);
14     if (out.fail())
15     {
16         //error
17     }
18     Json::StyledStreamWriter sswriter;
19     sswriter.write(out, root);
20     out.close();
21 }

生成:

{
    "ip" : "127.0.0.1",
    "port" : "8080"
}

Json嵌套数组,嵌套Json,数组可以通过下标来访问。

//数组Json
//嵌套json
void Config2()
{
    Json::Value root;
    Json::Reader reader;
    //
    root["name"] = Json::Value("karllen");
    root["age"] = Json::Value("24");
    
    //adress
    Json::Value adrress;
    adrress["province"] = "浙江";
    adrress["city"] = "杭州";
    adrress["district"] = "滨江区";
    root["address"] = adrress;

    //hoby array
    Json::Value hobby;
    hobby.append(Json::Value("足球"));
    hobby.append(Json::Value("篮球"));
    hobby.append(Json::Value("乒乓球"));
    root["hobby"] = hobby;
    //
    Json::Value education, education1, education2, education3;
    education1["school"] = Json::Value("大学");
    education1["period"] = Json::Value("2012-09-01~2016-06-01");
    education1["address"] = Json::Value("武汉");
    //
    education2["school"] = Json::Value("高中");
    education2["period"] = Json::Value("2009-09-01~2012-06-01");
    education2["address"] = Json::Value("山东");
    //
    education3["school"] = Json::Value("初中");
    education3["period"] = Json::Value("2006-09-01~2009-06-01");
    education3["address"] = Json::Value("山东");
    //
    education.append(education1);
    education.append(education2);
    education.append(education3);
    root["education"] = education;
    //
    std::ofstream out;
    out.open("config2.json", std::ios::binary);
    if (out.fail())
    {
        //error
    }
    Json::StyledStreamWriter sswriter;
    sswriter.write(out, root);
    out.close();
}

生成: 生成的Json由name,age,adress,education,hobby组成,address则是嵌套了一个json格式,education是嵌套了一个Json数组,

数组有三个Json标示教育阶段,hobby则是嵌套了一个数组,直接标示了内容。如下:

 1 {
 2     "address" : 
 3     {
 4         "city" : "杭州",
 5         "district" : "滨江区",
 6         "province" : "浙江"
 7     },
 8     "age" : "24",
 9     "education" : 
10     [
11         {
12             "address" : "武汉",
13             "period" : "2012-09-01~2016-06-01",
14             "school" : "大学"
15         },
16         {
17             "address" : "山东",
18             "period" : "2009-09-01~2012-06-01",
19             "school" : "高中"
20         },
21         {
22             "address" : "山东",
23             "period" : "2006-09-01~2009-06-01",
24             "school" : "初中"
25         }
26     ],
27     "hobby" : [ "足球", "篮球", "乒乓球" ],
28     "name" : "karllen"
29 }

 

解析上面的Json文件

 1 //Json读取
 2 //
 3 void Config3()
 4 {
 5     std::ifstream ins;
 6     ins.open("config2.json", std::ios::binary);
 7     if(ins.fail())
 8     {
 9         //error
10     }
11     Json::Value root;
12     Json::Reader reader;
13     reader.parse(ins, root);
14     ins.close();
15     //
16     std::vector<std::string> vecStrJson;
17     
18     vecStrJson.push_back(root["name"].asString());
19     vecStrJson.push_back(root["age"].asString());
20 
21     auto size = root["hobby"].size();
22     for (auto j = 0; j < size; j++)
23     {
24         vecStrJson.push_back(root["hobby"][j].asString());
25     }
26 
27     Json::Value address = root["address"];
28     vecStrJson.push_back(address["province"].asString());
29     vecStrJson.push_back(address["city"].asString());
30     vecStrJson.push_back(address["district"].asString());
31 
32     Json::Value education = root["education"];
33     size = education.size();
34     for (auto i = 0; i < size; i++)
35     {
36         vecStrJson.push_back(education[i]["school"].asString());
37         vecStrJson.push_back(education[i]["period"].asString());
38         vecStrJson.push_back(education[i]["address"].asString());
39     }
40 }

将所有的Json值都存到了vecStrJson中

技术分享

 

jsoncpp的使用