-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Impossible to do an array of composite objects #691
Comments
How should the JSON value look like in the end? I am not sure why you use quotes around the value you pass to push_back. |
It was just an example. I want to do somthing like this: json o;
json j;
o["objectId"] = 0 ;
o["transferData"]["transfer_id"] = 0;
o["transferData"]["transfer_time"] = 57;
j.push_back(o.dump());
o["objectId"] = 1 ;
o["transferData"]["transfer_id"] = 1;
o["transferData"]["transfer_time"] = 49;
j.push_back(o.dump()); the JSON should look like this: [ {
"objectId" : 0,
"transfer_data" : [
{
"transfer_id" : 0 ,
"transfer_time" : 57
}
]
} ,
{
"objectId" : 1,
"transfer_data" : [
{
"transfer_id" : 1 ,
"transfer_time" : 49
}
]
}
] |
You need to pass a This should produce the desired JSON: #include "json.hpp"
#include <iostream>
using json = nlohmann::json;
int main() {
json o;
json j;
o["objectId"] = 0 ;
o["transferData"]["transfer_id"] = 0;
o["transferData"]["transfer_time"] = 57;
j.push_back(o);
o["objectId"] = 1 ;
o["transferData"]["transfer_id"] = 1;
o["transferData"]["transfer_time"] = 49;
j.push_back(o);
std::cout << std::setw(2) << j << std::endl;
} |
You can also create the array elements like this: json j;
j.push_back(
{
{"objectId", 0},
{"transferData",
{
{"transfer_id", 0},
{"transfer_time", 57}
}
}
});
j.push_back(
{
{"objectId", 1},
{"transferData",
{
{"transfer_id", 1},
{"transfer_time", 49}
}
}
}); or even
|
Oh my bad! Thank you! |
You're welcome! |
I need to create a JSon array like this for example:
But the push_back function only acept initializer list that contains only two elements.
The text was updated successfully, but these errors were encountered: