-
-
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
Combine json objects into one? #1165
Comments
What have you tried? Could you show us some more elaborate code-examples of what you do and how you do it? To push an object into a list you can do: json a = json::array();
a.push_back({{"key", "value"}}); // two braces!
// or
json d;
d["key"] = value;
a.push_back(d); |
Hi thank you for the response! Packet["Id"] = 10;
typedef struct Movement_{
int XPosition;
int YPosition;
}Movement;
std::set<Movement> v;
for(int x = 0; x < 10; ++x)
{
Movement Mov = {0};
Mov.XPosition = x;
Mov.YPosition = x+1;
v.insert(Mov);
}
Packet["MovementPackets"] = v;```
Essentially I wanted one "Id" while the rest was a set of objects following it.
It seems like the code tags are being buggy on here. |
Before proposing something, could you write (by hand if needed) the json you would expect? Where do you see the array, where the object(s)? |
It was more of a question than a proposal if it already existed, but the JSON results I would have been expecting from the code above, to look something like this:
And so on until XPosition[9] and YPosition[9] But I'm thinking a way I can do this wiht the existing code is simply appending the string value of [0],[1] as I loop through? |
I meant before I can propose something. However, are you sure your JSON is efficient? Why not: {"Id": 10,
"Position": [
{ "x": 0, "y": 1 },
{ "x": 1, "y": 2 }
]
} |
I think the way you wrote it is far more efficient and better looking than I how I wrote it! |
Hmm, what do you mean with unique keys? Here is how you would access this with nlohmann-json: d["Id"] == 10
d["Position"][0]["x"] == 0
d["Position"][1]["y"] == 2 |
Would that need to be implemented or is there a workaround I can use for it? The other concern I have is, I'm sending this over through a Websocket to a javascript client and I'm not certain if the JSON implementation in Javascript would be compatible if the feature was implemented here. |
This is standard-compliant JSON: {"Id": 10,
"Position": [
{ "x": 0, "y": 1 },
{ "x": 1, "y": 2 }
]
} |
Ah okay. Thank you so much! I'll see if I can find a work around until then. |
Workaround for what? |
What you posted:
Oh I think I just realised what you did. You pushed back the previous json into the original. I get it now. I'm sorry for the confusion! |
Of course. #include <nlohmann/json.hpp>
#include <iostream>
int main(void)
{
nlohmann::json pos;
pos["Id"] = 10;
pos["Position"] = nlohmann::json::array();
for (int i = 0; i < 10; i++)
pos["Position"].push_back({{"x", i}, {"y", i + 1}});
std::cout << std::setw(2) << pos << "\n";
return 0;
} Prints {
"Id": 10,
"Position": [
{
"x": 0,
"y": 1
},
{
"x": 1,
"y": 2
},
{
"x": 2,
"y": 3
},
{
"x": 3,
"y": 4
},
{
"x": 4,
"y": 5
},
{
"x": 5,
"y": 6
},
{
"x": 6,
"y": 7
},
{
"x": 7,
"y": 8
},
{
"x": 8,
"y": 9
},
{
"x": 9,
"y": 10
}
]
} |
Thank you! That's exactly what I was trying to do! |
You're welcome. Don't forget to close this issue if you have no further problems regarding your initial questions. |
The title might not correctly describe what I'm attempting but essentially I have one structure that I can convert to a json string but I want to append a list of objects also to be converted into json strings and send that all together as one packet.
Something like this.
`json Packet;
Packet["PacketType"] = config::packet::type::TYPE;
Packet["ArrayOfObjects"] = ArrayOfObjects;`
Or make two separate json objects and combine them.
Another question I have is, does an array of objects have to be set at initialisation?
The text was updated successfully, but these errors were encountered: