-1

The json file is:

{
  "vis.DataSet": [],
  "backup": []
}

I want to insert 2 objects {a=1,b=2,c=3} and {a=4,b=5} to "vis.DataSet" key.

Then the json become as

{
  "vis.DataSet": [
  {
    "a" : 1,
    "b" : 2,
    "c" : 3
  },
  {
    "a" : 4,
    "b" : 5
  }
  ],
  "backup": []
}

Here is my code, but it doesn't work.

json srcJson = R"(
{
  "vis.DataSet": [],
  "backup": []
}
)"_json;

json objJson1 = R"(
{
  "a" : 1,
  "b" : 2,
  "c" : 3
}
)"_json;

json bojJson2 = R"(
{
  "a" : 4,
  "b" : 5
}
)"_json;

srcJson["vis.DataSet"].push_back[objJson1];
srcJson["vis.DataSet"].push_back[objJson2];

wohlstad
  • 12,661
  • 10
  • 26
  • 39
minjiew
  • 1
  • 1
  • What do you mean `with but it doesn't work.`? The shown code does not compile. Is your question about the compile errors? If so, you have to include those in the question and explain what you don't understand about them. – t.niese Sep 28 '22 at 10:31
  • You should include the exact comiplation error you get (as text) in your question. – wohlstad Sep 28 '22 at 10:45
  • Welcome to StackOverflow. If an answer solves your problem you could click '✔' to mark it as an accepted answer. With enough rep you can _also_ upvote any helpful answer (see here: https://stackoverflow.com/help/someone-answers). – wohlstad Sep 28 '22 at 15:32

1 Answers1

2

In lines like this:

srcJson["vis.DataSet"].push_back[objJson1];

You attempt to call the push_back method with square brackets ([]) instead of normal parenthesis (()).
Therefore you get compilation errors.

You need to change it to:

//------------------------------V--------V
srcJson["vis.DataSet"].push_back(objJson1);

The same applies to the second attempt to call push_back.

wohlstad
  • 12,661
  • 10
  • 26
  • 39