I am using jsoncpp to write the data into json format like following:
Json::Value event;
Json::Value lep(Json::arrayValue);
event["Lepton"] = lep;
lep.append(Json::Value(1));
lep.append(Json::Value(2));
lep.append(Json::Value(3));
lep.append(Json::Value(4));
event["Lepton"] = lep;
Json::StyledWriter styledWriter;
cout << styledWriter.write(event);
I got the following output:
{
"Lepton" : [
1,
2,
3,
4
]
}
I want to write multiple such blocks into my data files. What I eventually want is following:
[
{
"Lepton" : [
1,
2,
3,
4
]
},
{
"Lepton" : [
1,
2,
3,
4
]
}
]
Currently, I am writing [
and then the json entries followed by a ,
and finally at the end ]
. Also I have to remove the last ,
in the final data file.
Is there a way to do all this automatically by jsoncpp or by other means?
Thanks
Using the suggestion from @Some prorammer dude in the comments section, I did the following:
Json::Value AllEvents(Json::arrayValue);
for(int entry = 1; entry < 3; ++entry)
{
Json::Value event;
Json::Value lep(Json::arrayValue);
lep.append(Json::Value(1 + entry));
lep.append(Json::Value(2 + entry));
lep.append(Json::Value(3 + entry));
lep.append(Json::Value(4 + entry));
event["Lepton"] = lep;
AllEvents.append(event);
Json::StyledWriter styledWriter;
cout << styledWriter.write(AllEvents);
}
I got the desired output as shown below:
[
{
"Lepton" : [
1,
2,
3,
4
]
},
{
"Lepton" : [
2,
3,
4,
5
]
}
]
Basically, I created a Json array and appended the resulting Json objects into it.