在新的Jsoncpp中 Json::Writer、Json::FastWriter、Json::Reader 等都被摒弃了,
用了新的Json::StreamWriterBuilder、Json::CharReaderBuilder代替。
下面是示例用法:
1、把现有数据拼装成Json字符串
string strRes = "";
Json::Value root;
Json::StreamWriterBuilder jsrocd;
std::unique_ptr<Json::StreamWriter> write(jsrocd.newStreamWriter());
ostringstream os;
root["Name"] = "张三";
root["Age"] = 20;
write->write(root, &os);
strRes = os.str();
2、解析Json字符串(直接拿上面的strRes来用了。)
int nLen = strRes.length();
Json::Value root;
Json::CharReaderBuilder jsreader;
std::unique_ptr<Json::CharReader> const reader(jsreader.newCharReader());
const char *pStart = record.c_str();
string err;
//失败
if (!reader->parse(pStart, pStart + nLen, &root, &err))
return false;
string strName = root["Name"].asString();
int strAge = root["Age"].asInt();