1.使用环境DevC++

a.建立C++工程,并添加.\JsonCPP\jsoncpp-master\jsoncpp-master\src\lib_json中源文件到工程中。

b.添加头文件路径

2.使用实例

a.主函数

 1 #include <iostream>
 2 #include <json/json.h>
 3 using namespace std;
 4  
 5 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 6 void readJson(string strValue);
 7 string writeJson();
 8 
 9 int main(int argc, char** argv) {
10     
11     cout<<"-------------Construct Json!-------------"<<endl;
12     std::string json = writeJson();    
13     
14     cout<<"-------------Resolve Json!--------------"<<endl;
15     readJson(json);
16 
17     return 0;
18 }
View Code

b.构造json 

 1 //构造json格式内容 
 2 std::string writeJson() {
 3     using namespace std;
 4     
 5     //Value类似一个容器,可以添加多个键值对的元素 
 6     Json::Value root;
 7     Json::Value arrayObj;
 8     Json::Value item1;
 9     Json::Value item2;
10     Json::Value item3;
11     
12     item1["cpp"] = "jsoncpp"; 
13     arrayObj.append(item1);//添加一对大括号 
14     item2["java"] = "jsoninjava";
15     arrayObj.append(item2);
16     item3["php"] = "support";
17     arrayObj.append(item3);
18 
19     root["name"] = "json";
20     root["type"] = 8;
21     root["bool"] = 0;
22     root["array"] = arrayObj;//添加一对中括号 
23 
24     //root.toStyledString();//Json格式存储之后,转化为Json格式字符串 
25     std::string out = root.toStyledString();
26     std::cout << out << std::endl;
27     return out;
28 }
View Code

c.解析Json

 1 //解析Json格式内容 
 2 void readJson(string strValue) {
 3     using namespace std;
 4     //std::string strValue = "{\"name\":\"json\",\"type\":3,\"bool\":true,\
 5 //          \"array\":[{\"cpp\":\"jsoncpp\"},{\"java\":\"jsoninjava\"},{\"php\":\"support\"}]}";
 6 
 7     Json::Reader reader;
 8     Json::Value value;
 9     
10     //自动解析strValue中Json内容,并存储到Value中 
11     if (reader.parse(strValue, value))
12     {   
13         //字符串类型的成员 
14         std::string out = value["name"].asString();//get value of the key --nm 
15         std::cout <<"name:"<< out << std::endl;
16         //整形类型成员 
17            int num = value["type"].asInt();//get value of the key --nm 
18         std::cout <<"type:"<< num << std::endl;     
19         
20            bool bl = value["bool"].asBool();//get value of the key --nm 
21         std::cout <<"bool:"<< bl << std::endl;//bool值输出是0或者1 
22              
23         /* Error --nm
24         std::string ar = value["array"].asString();//get value of the key --nm 
25         std::cout << ar << std::endl;
26         */
27         //数组类型的成员 
28         const Json::Value arrayObj = value["array"];
29         for(unsigned j = 0;j < arrayObj.size(); j++){
30                   //判断是否含有该键
31             if (arrayObj[j].isMember("cpp")){
32                  //取出给定键对应的值
33                 out = arrayObj[j]["cpp"].asString();
34                 std::cout <<"cpp:"<< out << std::endl;
35             }else if (arrayObj[j].isMember("java")){
36                 out = arrayObj[j]["java"].asString();
37                 std::cout <<"java:"<< out << std::endl;
38             }else if (arrayObj[j].isMember("php")){
39                 out = arrayObj[j]["php"].asString();
40                 std::cout <<"php:"<< out << std::endl;
41             }
42         }
43     }   
44 }
View Code

d结果

posted on 2017-04-14 11:17  人生一世,草木一秋。  阅读(325)  评论(0编辑  收藏  举报