1 jsoncpp的api简要说明
1,解析(json字符串转为对象)
std::string strDataJson;
Json::Reader JReader;
Json::Value JObject;
if (!JReader.parse(strDataJson, JObject))
{
cerr << "parse json error." << endl;
return bSuccess;
}
2,读取
std::string strMsg = JRec["msg"].asString();
int nRetCode = JRec["ret"]..asInt();
Json::Value JList = JRec["data"]["list"];
int nSize = JList.size();
获取错误信息: JReader.getFormatedErrorMessages()
3,增加或修改
JRoot["stringdata"] = Json::Value("msg");
JRoot["intdata"] = Json::Value(10);
4,删除
JValue.removeMember("toberemove");
5,对象转为字符串
//输出无格式json字符串
Json::FastWriter fast_writer;
strJRecList = fast_writer.write(JRoot);
//格式化之后的json,有回车换行符
std::string strOut = JRoot.toStyledString();
时间: 2024-10-31 00:48:21