山姆供大家参考PLE代码,如下:
file.json
{
"B":"b_val2",
"A":{
"AA":"aa_val1",
"AAA" : "aaa_val2",
"AAAA" : "aaaa_val3"
},
"C":"c_val3",
"D":"d_val4"
}
jsoncpp使用场景如下,供上述样品JSON文件。
#include <iostream>
#include "json/json.h"
#include <fstream>
using namespace std;
int main(){
Json::Value root;
Json::Reader reader;
const Json::Value defValue; //used for default reference
std::ifstream ifile("file.json");
bool isJsonOK = (ifile != NULL && reader.parse(ifile, root));
if(isJsonOK){
const Json::Value s = root.get("A",defValue);
if(s.isObject()){
Json::Value s2 = s.get("AAA","");
cout << "s2 : " << s2.asString() << endl;
}else{
cout << "value for key \"A\" is not object type !" << endl;
}
}
else
cout << "json not OK !!" << endl;
return 1;
}
输出::
S2:aaa_val2
此外,我已经使用了"amalgamate.py"用于产生和使用所述jsoncpp对于样品源上方。
是啊,这就是我要找的;我不知道如何在缓冲区上使用reader.parse()。更新OP – KaiserJohaan
我认为使用缓冲区而不是字符串是没有问题的,但是如果你有'char buffer [max]'之类的东西,你可以使用'parse(buffer,buffer + max-1,values)'。 – deepmax