Linux C/C++ [CPP_json]
Linux C/C++ [CPP_json]
資料來源: https://github.com/jash-git/Jash_LinuxC/tree/master/CPP_json
jsoncpp [原廠]
makefile ~ 標準簡易看得懂得的標準參考範例
Win_Linux_CPP_Json [自己CB專案範例]
code:
#include <string> #include "json.h" void readJson(); void writeJson(); int main(int argc, char** argv) { readJson(); writeJson(); return 0; } void readJson() { using namespace std; std::string strValue = "{\"name\":\"json\",\"array\":[{\"cpp\":\"jsoncpp\"},{\"java\":\"jsoninjava\"},{\"php\":\"support\"}]}"; Json::Reader reader; Json::Value value; if (reader.parse(strValue, value)) { std::string out = value["name"].asString(); std::cout << out << std::endl; const Json::Value arrayObj = value["array"]; for (unsigned int i = 0; i < arrayObj.size(); i++) { if (!arrayObj[i].isMember("cpp")) continue; out = arrayObj[i]["cpp"].asString(); std::cout << out; if (i != (arrayObj.size() - 1)) std::cout << std::endl; } } } void writeJson() { using namespace std; Json::Value root; Json::Value arrayObj; Json::Value item; item["cpp"] = "jsoncpp"; item["java"] = "jsoninjava"; item["php"] = "support"; arrayObj.append(item); root["name"] = "json"; root["array"] = arrayObj; root.toStyledString(); std::string out = root.toStyledString(); std::cout << out << std::endl; }