json:
{
"shemename":"方案",
"eventname":"事件",
"name":"lucy"
"lucy":
{
"age":17,
"gender":"female"
}
"course":
[
"math",
"chinese",
"English"
]
}
bool readJson()
{
bool res;
JSONCPP_STRING errs;
Json::Value root;
Json::CharReaderBuilder readerBuilder;
std::unique_ptr<Json::CharReader> reader(readerBuilder.newCharReader());
res = reader->parse(inputJson.c_str(), inputJson.c_str()+inputJson.length(), &root, &errs);
if (!res || !errs.empty()) {
std::cout << "js串解析异常 " << errs << std::endl;
return false;
}
std::cout<<root["shemename"].asString()<<std::endl;
std::cout<<root["eventname"].asString()<<std::endl;
std::cout<<root["name"].asString()<<std::endl;
Json::Value lucy=root["lucy"];
std::cout<<lucy["age"].asInt()<<std::endl;
std::cout<<lucy["gender"].asString()<<std::endl;
Json::Value course=root["course"];
std::cout<<"course";
for(int i=0;i<course.size();++i)
{
std::cout<<course[i]<<std::endl;
}
return true;
}
Int asInt() const;
UInt asUInt() const;
Int64 asInt64() const;
UInt64 asUInt64() const;
LargestInt asLargestInt() const;
LargestUInt asLargestUInt() const;
float asFloat() const;
double asDouble() const;
bool asBool() const;
方法二:
Json::CharReaderBuilder b;
Json::CharReader* reader(b.newCharReader());
Json::Reader reader;
JSONCPP_STRING jsErStr;
Json::Value root;
std::string aaa, bbb, ccc;
if (reader->parse(str_input.c_str(), str_input.c_str()+std::strlen(str_input.c_str()),&root, &jsErStr))
{
aaa = root["xxx"].asString();
bbb = root["xxx"].asString();
ccc = root["xxx"].asString();
}
delete reader;