JsonCPP 使用(VC++ 解析Json)
项目中加载.h 和 lib
测试项目在G:\colibri\DDM\DDM
#include <string> #include "JsonCpp\json\json.h" void CControl::readJson() { 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(); 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(); } } } //生成JSON格式数据 void CControl::writeJson() { 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; //std::string out = root.toStyledString(); //格式化 Json::FastWriter writer; std::string strWrite = writer.write(root); AfxMessageBox(strWrite.c_str()); int len = strWrite.length(); m_MesClient.SendClient((char*)strWrite.c_str(), len); /*std::ofstream ofs; ofs.open("test_write.json"); ofs << strWrite; ofs.close(); std::cout << out << std::endl;*/ }
posted on 2020-07-27 16:58 strangeman 阅读(1382) 评论(0) 编辑 收藏 举报