1 jsoncpp的api简要说明
1,解析(json字符串转为对象)
std::stringstrDataJson;
Json::ReaderJReader;
Json::ValueJObject;
if(!JReader.parse(strDataJson,JObject))
{
cerr<<"parsejsonerror."<<endl;
returnbSuccess;
}
2,读取
std::stringstrMsg=JRec["msg"].asString();
intnRetCode=JRec["ret"]..asInt();
Json::ValueJList=JRec["data"]["list"];
intnSize=JList.size();
获取错误信息:JReader.getFormatedErrorMessages()
JRoot["stringdata"]=Json::Value("msg");
JRoot["intdata"]=Json::Value(10);
4,删除
JValue.removeMember("toberemove");
5,对象转为字符串
//输出无格式json字符串
Json::FastWriterfast_writer;
strJRecList=fast_writer.write(JRoot);
//格式化之后的json,有回车换行符
std::stringstrOut=JRoot.toStyledString();
转自;http://my.oschina.net/chenleijava/blog/144312
6各种json子类型的使用
(1)json Object
- for(Json::ValueIteratoriter=groups_config.begin();iter!=groups_config.end();iter++){
- Json::Valueclient_dict=(*iter)["clients"];
- stringtopic_recresult=(*iter)["topics"]["recresult"].asString();
- stringtopic_recreq=(*iter)["topics"]["recreq"].asString();
- stringtopic_input=(*iter)["topics"]["input"].asString();
- for(Json::ValueIteratorclient_iter=client_dict.begin();client_iter!=client_dict.end();++client_iter){
- stringcid=client_iter.key().asString();
- //FIXME:createanewcustomerconfiguration
- CustomerPtrc(newCustomer);
- c->LoadFromJson(cid,(*client_iter),topic_recresult,topic_recreq,topic_input);
- customers[cid]=c;
- }
- }
2 详细API