Skip to content

How to add an Object to an Array? #2985

Answered by nlohmann
MaybeELEMENT asked this question in Q&A
Discussion options

You must be logged in to vote

The object function expects a list of pairs (see https://json.nlohmann.me/api/basic_json/object/). You could also use the second version of push_back and provide an object value directly (see https://json.nlohmann.me/api/basic_json/push_back/).

For your example:

res.push_back(json::object({{"label", "key"}, {"value", "var"}}));
res.push_back(json::object({{"label", "string"}, {"value", "hello world"}}));

You can also skip the json::object here, because list of pairs are treated as object anyway:

res.push_back({{"label", "key"}, {"value", "var"}});
res.push_back({{"label", "string"}, {"value", "hello world"}});

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by MaybeELEMENT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants