Doubt how to get a JSON, transform into object

0

I have a little doubt about a json module in c ++, I was wondering if there is any way to get a json that already exists and increment more fields using c ++,

json data = json::parse(getData());
FILE * pFile;
pFile = fopen("data.json", "a");
fputs("", pFile);
fclose(pFile);

{
"0": {
"area": "SI",
"codigo_curso": "01",
"cpf": "000.000.000-00",
"data_nascimento": "00/00/0000",
"descricao": "Breve",
"endereco": "00000-000",
"nome": "Daniel Yohan"
}
}

I wanted to add more of these here with changed values:

"0": {
"area": "SI",
"codigo_curso": "01",
"cpf": "000.000.000-00",
"data_nascimento": "00/00/0000",
"descricao": "Breve",
"endereco": "00000-000",
"nome": "Daniel Yohan"
}

I'm using the link

Would anyone know how to load my current json then add and save again? I'll be grateful, this is for a college job. Thanks

    
asked by anonymous 26.03.2017 / 07:41

1 answer

1

I believe that as the README is thus link , it would look something like this:

json newitem = {
"area": "SI",
"codigo_curso": "02",
"cpf": "000.000.000-00",
"data_nascimento": "00/00/0000",
"descricao": "Breve",
"endereco": "00000-000",
"nome": "João"
};

json data = json::parse(getData());
data["1"] = newitem;

In order to write, I would change the a to w , or else I would change FILE by ofstream so I do not have to do conversions and use dump() (or .dump(4) to put spaces) / p>

string jsonstr = j.dump();

ofstream arq;
arq.open("data.json");
arq << jsonstr;
arq.close();

This will update the current file

    
26.03.2017 / 18:48