jsoncpp的安装与使用示例
安装:
生成静态库
1 生成静态库: 2 第一步:生成目标文件: 3 g++ -g -Wall -c json_reader.cpp json_value.cpp json_writer.cpp -I. -I../../include -lpthread 4 5 会生成三个*.o文件 6 json_reader.o 7 json_value.o 8 json_writer.o 9 10 第二步:生成静态库文件: 11 ar rcs libjsoncpp.a *.o
生成动态库:
g++ -g json_reader.cpp json_value.cpp json_writer.cpp -fPIC -Wall -shared -o libjsoncpp.so -I. -I../../include -lpthread
记得引用时:
1.指定库文件:比如将生成的 libjsoncpp.so和libjsoncpp.a库文件放到/usr/local/lib/jsoncpp-src-0.5.0/下面.
2.指定头文件位置:比如,将jsoncpp-src-0.5.0/include/下的头文件放到/usr/local/include/jsoncpp-src-0.5.0/下面.
实用示例:
1 #include <iostream> 2 #include <fstream> 3 4 //#include </usr/local/include/jsoncpp0.5.0/json/json.h> 5 //#include <json/json.h> 6 #include <json/json.h> 7 using namespace std; 8 9 int main() 10 { 11 //根节点 12 Json::Value root; 13 14 //根节点属性 15 root["name"] = Json::Value("Tsybius"); 16 root["age"] = Json::Value(23); 17 root["sex_is_male"] = Json::Value(true); 18 19 //子节点 20 Json::Value partner; 21 22 //子节点属性 23 partner["partner_name"] = Json::Value("Galatea"); 24 partner["partner_age"] = Json::Value(21); 25 partner["partner_sex_is_male"] = Json::Value(false); 26 27 //子节点挂到根节点上 28 root["partner"] = Json::Value(partner); 29 30 //数组形式 31 root["achievement"].append("ach1"); 32 root["achievement"].append("ach2"); 33 root["achievement"].append("ach3"); 34 35 //直接输出 36 cout << "FastWriter:" << endl; 37 Json::FastWriter fw; 38 cout << fw.write(root) << endl << endl; 39 40 //缩进输出 41 cout << "StyledWriter:" << endl; 42 Json::StyledWriter sw; 43 cout << sw.write(root) << endl << endl; 44 45 //输出到文件 46 ofstream os; 47 os.open("./PersonalInfo.js"); 48 os << sw.write(root); 49 os.close(); 50 51 return 0; 52 }
output:
FastWriter: {"achievement":["ach1","ach2","ach3"],"age":23,"name":"Tsybius","partner":{"partner_age":21,"partner_name":"Galatea","partner_sex_is_male":false},"sex_is_male":true} StyledWriter: { "achievement" : [ "ach1", "ach2", "ach3" ], "age" : 23, "name" : "Tsybius", "partner" : { "partner_age" : 21, "partner_name" : "Galatea", "partner_sex_is_male" : false }, "sex_is_male" : true }
1 #include <bits/stdc++.h> 2 //#include <json/json.h> 3 #include </usr/include/jsoncpp/json/json.h> 4 void readJson(); 5 void writeJson(); 6 7 int main(int argc, char **argv) 8 { 9 readJson(); 10 writeJson(); 11 return 0; 12 } 13 14 void readJson() 15 { 16 using namespace std; 17 std::string strValue = 18 "{\"name\":\"json\",\"array\":[{\"cpp\":\"jsoncpp\"},{\"java\":\"jsoninjava\"},{\"php\":\"support\"}]}"; 19 20 Json::Reader reader; 21 Json::Value value; 22 23 if (reader.parse(strValue, value)) { 24 std::string out = value["name"].asString(); 25 std::cout << out << std::endl; 26 const Json::Value arrayObj = value["array"]; 27 for (unsigned int i = 0; i < arrayObj.size(); i++) { 28 if (!arrayObj[i].isMember("cpp")) 29 continue; 30 out = arrayObj[i]["cpp"].asString(); 31 std::cout << out; 32 if (i != (arrayObj.size() - 1)) 33 std::cout << std::endl; 34 } 35 } 36 } 37 38 void writeJson() 39 { 40 using namespace std; 41 42 Json::Value root; 43 Json::Value arrayObj; 44 Json::Value item; 45 46 item["cpp"] = "jsoncpp"; 47 item["java"] = "jsoninjava"; 48 item["php"] = "support"; 49 arrayObj.append(item); 50 51 root["name"] = "json"; 52 root["array"] = arrayObj; 53 54 root.toStyledString(); 55 std::string out = root.toStyledString(); 56 std::cout << out << std::endl; 57 }
1 json 2 jsoncpp 3 { 4 "array" : [ 5 { 6 "cpp" : "jsoncpp", 7 "java" : "jsoninjava", 8 "php" : "support" 9 } 10 ], 11 "name" : "json" 12 }