背景:
需要对Json文档按照特定字段进行提取,而在同一个字段下存在json对象数组。
代码:
<pre name="code" class="cpp"> string filename = "JsonData.txt";
ifstream infile(filename.c_str());
ofstream Outfile("Jdataresult.txt");
Json::Reader reader;
Json::Value ReadData;
string oneline;
Outfile<<"hashape"<<'\t'<<"hash3"<<'\t'<<"hash1"<<'\t'<<"hash"<<'\t'<<"hash_f"<<'\t'<<"scid"<<'\t'<<name1"<<'\t'<<"name2"<<endl;
while(getline(infile, oneline))
{
if(!reader.parse(oneline, ReadData))
{
return;
}
Json::Value subclass = ReadData["data"];//该字段的内容是个矩阵,而矩阵也是json对象。<span style="display: none; width: 0px; height: 0px;" id="transmark"></span>
int jsonsize = subclass.size();
cout<<"任务数:"<<jsonsize<<endl;
for(int i=0; i<jsonsize; ++i)
{
string hashape = subclass[i]["hash_ape"].asString();
string hash3 = subclass[i]["hash_3"].asString();
string hash1 = subclass[i]["hash_1"].asString();
string hash = subclass[i]["hash"].asString();
string hash_f = subclass[i]["hash_f"].asString();
int scid = subclass[i]["scid"].asInt();
string name1 = subclass[i]["name1"].asString();
string name2 = subclass[i]["name2"].asString();
Outfile<<hashape<<'\t'<<hash3<<'\t'<<hash1<<'\t'<<hash<<'\t'<<hash_f<<'\t'<<Uint32ToString(scid)<<'\t'<<name1<<'\t'<<name2<<endl;
}
}
Outfile.close();