jsoncpp 从文件读取json
代码如下
json文件如下
{
"addr" : "123456789abc",
"classinfo" : [
{
"index" : 1,
"type" : "3D"
},
{
"index" : 2,
"type" : "jingpin"
}
]
}
Json::Reader reader; // 解析json用Json::Reader
Json::Value root; // Json::Value是一种很重要的类型,可以代表任意类型。如int, string, object, array
cout<<filename<<endl;
ifstream in(filename,ios::binary);
if (reader.parse(in,root)) {
//if (!root["addr"].isNull()) {
cout << root["addr"].asString() << endl; ///读取元素
Json::Value arrayObj = root["classinfo"];
for (int i = 0; i < arrayObj.size(); i++) {
cout << arrayObj[i]["index"].asInt() << " "<<arrayObj[i]["type"].asString()<<endl;
}
//}
} else{
cout<<reader.getFormatedErrorMessages()<<endl;
}
in.close();
return 0;
运行之后报如下错误:
经过查找发现主要是content.json编码格式问题 嵌入式linux 需要使用utf8编码的文件或asn1编码 使用其他编码会出问题 尤其是在windows下生成的文件要注意生成json文件的编码方式