1

I'm inserting lot of different values to QJsonObjects like this:

//gender inserted to QJsonObject gender
QJsonObject gender;
gender.insert("gender", person->gender());

//birthDate inserted to QJsonObject birthDate
QJsonObject birthDate;
birthDate.insert("birthDate", person->birthdate().toString());

After that I'm appending QJsonObjects to QJsonArray like this:

//Inserting all objects to QJsonDocument m_jsonDocument
QJsonArray allObjects;
allObjects.append(gender);
allObjects.append(birthDate);

Then im putting it all to QJsonDocument:

m_jsonDocument->setArray(allObjects);

Output:

[{
    "gender": "male"
},
{
    "birthDate": "2001-12-19"
}]

What I need is to remove curly brackets around objects and replace square brackets with curly brackets. Do I need to put these to QString and remove and replace or is there an easier way to modify either objects, arrays or whole document? I tried to look around but didn'n find the right solution yet.

This is how I would like to see the output:

{
"gender": "male",
"birthDate": "2001-12-19"}

There is a lot of stuff inside objects and it needs to be as a FHIR standard. There is objects inside objects and the document still needs lot of modifying.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Lauri
  • 137
  • 1
  • 2
  • 8

1 Answers1

1

You should only use a single QJsonObject and add the properties to that object:

QJsonObject obj;
obj.insert("gender",  person->gender());
obj.insert("birthDate", person->birthdate().toString());
m_jsonDocument->setObject(obj);

Output:

{
    "birthDate": "2001-12-19",
    "gender": "male"
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thanks a lot eyllanec! Thant solved my problem with the brackets! Is there an easy way to effect to the order of objects inside object? At the moment using this method messes up the order of my objects after i set object to document. – Lauri Aug 29 '18 at 08:14
  • @Lauri The json is a dictionary and is organized alphabetically so that the access time is the minimum, so is the standard. The order in a json is not important for the APIs, so do not worry, your API will understand it correctly. – eyllanesc Aug 29 '18 at 08:18
  • Thanks! Yep it was alphabetically organized. API does not seem to care. That's great! Thanks again! – Lauri Aug 29 '18 at 08:58