c++, JSON, get object members in an array
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am using nlohmann::json
for parsing json in the program.
given a json there is an array with several objects, according to one of the object members I want to get other members of the same object.
like in the json below
{
"arr":[
{"a":1, "b":11, "c":111, ...},
{"a":2, "b":22, "c":222, ...},
{"a":3, "b":33, "c":333, ...},
...
]
}
for instance if the value of a
is 2
, I want to get the values of b,c,... of the same index/object.
currently I am using a for loop and at the index that j["arr"][i]["a"].get<int> == 2
going for the rest of members. As the array might have hundreds of members it is nonsense.
what is the best approach in this case?
c++ json nlohmann-json
add a comment |
I am using nlohmann::json
for parsing json in the program.
given a json there is an array with several objects, according to one of the object members I want to get other members of the same object.
like in the json below
{
"arr":[
{"a":1, "b":11, "c":111, ...},
{"a":2, "b":22, "c":222, ...},
{"a":3, "b":33, "c":333, ...},
...
]
}
for instance if the value of a
is 2
, I want to get the values of b,c,... of the same index/object.
currently I am using a for loop and at the index that j["arr"][i]["a"].get<int> == 2
going for the rest of members. As the array might have hundreds of members it is nonsense.
what is the best approach in this case?
c++ json nlohmann-json
add a comment |
I am using nlohmann::json
for parsing json in the program.
given a json there is an array with several objects, according to one of the object members I want to get other members of the same object.
like in the json below
{
"arr":[
{"a":1, "b":11, "c":111, ...},
{"a":2, "b":22, "c":222, ...},
{"a":3, "b":33, "c":333, ...},
...
]
}
for instance if the value of a
is 2
, I want to get the values of b,c,... of the same index/object.
currently I am using a for loop and at the index that j["arr"][i]["a"].get<int> == 2
going for the rest of members. As the array might have hundreds of members it is nonsense.
what is the best approach in this case?
c++ json nlohmann-json
I am using nlohmann::json
for parsing json in the program.
given a json there is an array with several objects, according to one of the object members I want to get other members of the same object.
like in the json below
{
"arr":[
{"a":1, "b":11, "c":111, ...},
{"a":2, "b":22, "c":222, ...},
{"a":3, "b":33, "c":333, ...},
...
]
}
for instance if the value of a
is 2
, I want to get the values of b,c,... of the same index/object.
currently I am using a for loop and at the index that j["arr"][i]["a"].get<int> == 2
going for the rest of members. As the array might have hundreds of members it is nonsense.
what is the best approach in this case?
c++ json nlohmann-json
c++ json nlohmann-json
asked Nov 16 '18 at 16:36
Amir-MousaviAmir-Mousavi
86111238
86111238
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
It's a JSON array, you need to iterate over it. So your approach is the simple and direct one.
add a comment |
Let's call the C++ type of the elements of arr
Thing
, you can convert arr
to a std::vector<Thing>
.
void to_json(nlohmann::json & j, const Thing & t) {
j = nlohmann::json{{"a", t.a}, {"b", t.b}, {"c", t.c}}; // similarly other members ...
}
void from_json(const nlohmann::json & j, Thing & t) {
j.at("a").get_to(t.a);
j.at("b").get_to(t.b);
j.at("c").get_to(t.c); // similarly other members ...
}
std::vector<Thing> things = j["arr"];
auto it = std::find_if(things.begin(), things.end(), (const Thing & t){ return t.a ==2; });
// use it->b etc
add a comment |
x2struct(https://github.com/xyz347/x2struct) can load json to struct with condition.
#include "x2struct.hpp"
#include <iostream>
using namespace std;
struct Item {
int a;
int b;
int c;
XTOSTRUCT(O(a,b,c));
XTOSTRUCT_CONDITION() { // load only this return true
int a = -1;
if (obj.has("a")) {
obj["a"].convert(a);
}
return a==2;
}
};
struct Test {
Item arr;
XTOSTRUCT(O(arr));
};
int main(int argc, char *argv) {
string jstr = "{"arr":[{"a":1, "b":11, "c":111},{"a":2, "b":22, "c":222},{"a":3, "b":33, "c":333}]}";
Test t;
x2struct::X::loadjson(jstr, t, false);
cout<<t.arr.b<<','<<t.arr.c<<endl;
}
output is:
22,222
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341987%2fc-json-get-object-members-in-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's a JSON array, you need to iterate over it. So your approach is the simple and direct one.
add a comment |
It's a JSON array, you need to iterate over it. So your approach is the simple and direct one.
add a comment |
It's a JSON array, you need to iterate over it. So your approach is the simple and direct one.
It's a JSON array, you need to iterate over it. So your approach is the simple and direct one.
answered Nov 19 '18 at 9:35
riokirioki
4,16442448
4,16442448
add a comment |
add a comment |
Let's call the C++ type of the elements of arr
Thing
, you can convert arr
to a std::vector<Thing>
.
void to_json(nlohmann::json & j, const Thing & t) {
j = nlohmann::json{{"a", t.a}, {"b", t.b}, {"c", t.c}}; // similarly other members ...
}
void from_json(const nlohmann::json & j, Thing & t) {
j.at("a").get_to(t.a);
j.at("b").get_to(t.b);
j.at("c").get_to(t.c); // similarly other members ...
}
std::vector<Thing> things = j["arr"];
auto it = std::find_if(things.begin(), things.end(), (const Thing & t){ return t.a ==2; });
// use it->b etc
add a comment |
Let's call the C++ type of the elements of arr
Thing
, you can convert arr
to a std::vector<Thing>
.
void to_json(nlohmann::json & j, const Thing & t) {
j = nlohmann::json{{"a", t.a}, {"b", t.b}, {"c", t.c}}; // similarly other members ...
}
void from_json(const nlohmann::json & j, Thing & t) {
j.at("a").get_to(t.a);
j.at("b").get_to(t.b);
j.at("c").get_to(t.c); // similarly other members ...
}
std::vector<Thing> things = j["arr"];
auto it = std::find_if(things.begin(), things.end(), (const Thing & t){ return t.a ==2; });
// use it->b etc
add a comment |
Let's call the C++ type of the elements of arr
Thing
, you can convert arr
to a std::vector<Thing>
.
void to_json(nlohmann::json & j, const Thing & t) {
j = nlohmann::json{{"a", t.a}, {"b", t.b}, {"c", t.c}}; // similarly other members ...
}
void from_json(const nlohmann::json & j, Thing & t) {
j.at("a").get_to(t.a);
j.at("b").get_to(t.b);
j.at("c").get_to(t.c); // similarly other members ...
}
std::vector<Thing> things = j["arr"];
auto it = std::find_if(things.begin(), things.end(), (const Thing & t){ return t.a ==2; });
// use it->b etc
Let's call the C++ type of the elements of arr
Thing
, you can convert arr
to a std::vector<Thing>
.
void to_json(nlohmann::json & j, const Thing & t) {
j = nlohmann::json{{"a", t.a}, {"b", t.b}, {"c", t.c}}; // similarly other members ...
}
void from_json(const nlohmann::json & j, Thing & t) {
j.at("a").get_to(t.a);
j.at("b").get_to(t.b);
j.at("c").get_to(t.c); // similarly other members ...
}
std::vector<Thing> things = j["arr"];
auto it = std::find_if(things.begin(), things.end(), (const Thing & t){ return t.a ==2; });
// use it->b etc
answered Nov 19 '18 at 10:16
CalethCaleth
18.8k22142
18.8k22142
add a comment |
add a comment |
x2struct(https://github.com/xyz347/x2struct) can load json to struct with condition.
#include "x2struct.hpp"
#include <iostream>
using namespace std;
struct Item {
int a;
int b;
int c;
XTOSTRUCT(O(a,b,c));
XTOSTRUCT_CONDITION() { // load only this return true
int a = -1;
if (obj.has("a")) {
obj["a"].convert(a);
}
return a==2;
}
};
struct Test {
Item arr;
XTOSTRUCT(O(arr));
};
int main(int argc, char *argv) {
string jstr = "{"arr":[{"a":1, "b":11, "c":111},{"a":2, "b":22, "c":222},{"a":3, "b":33, "c":333}]}";
Test t;
x2struct::X::loadjson(jstr, t, false);
cout<<t.arr.b<<','<<t.arr.c<<endl;
}
output is:
22,222
add a comment |
x2struct(https://github.com/xyz347/x2struct) can load json to struct with condition.
#include "x2struct.hpp"
#include <iostream>
using namespace std;
struct Item {
int a;
int b;
int c;
XTOSTRUCT(O(a,b,c));
XTOSTRUCT_CONDITION() { // load only this return true
int a = -1;
if (obj.has("a")) {
obj["a"].convert(a);
}
return a==2;
}
};
struct Test {
Item arr;
XTOSTRUCT(O(arr));
};
int main(int argc, char *argv) {
string jstr = "{"arr":[{"a":1, "b":11, "c":111},{"a":2, "b":22, "c":222},{"a":3, "b":33, "c":333}]}";
Test t;
x2struct::X::loadjson(jstr, t, false);
cout<<t.arr.b<<','<<t.arr.c<<endl;
}
output is:
22,222
add a comment |
x2struct(https://github.com/xyz347/x2struct) can load json to struct with condition.
#include "x2struct.hpp"
#include <iostream>
using namespace std;
struct Item {
int a;
int b;
int c;
XTOSTRUCT(O(a,b,c));
XTOSTRUCT_CONDITION() { // load only this return true
int a = -1;
if (obj.has("a")) {
obj["a"].convert(a);
}
return a==2;
}
};
struct Test {
Item arr;
XTOSTRUCT(O(arr));
};
int main(int argc, char *argv) {
string jstr = "{"arr":[{"a":1, "b":11, "c":111},{"a":2, "b":22, "c":222},{"a":3, "b":33, "c":333}]}";
Test t;
x2struct::X::loadjson(jstr, t, false);
cout<<t.arr.b<<','<<t.arr.c<<endl;
}
output is:
22,222
x2struct(https://github.com/xyz347/x2struct) can load json to struct with condition.
#include "x2struct.hpp"
#include <iostream>
using namespace std;
struct Item {
int a;
int b;
int c;
XTOSTRUCT(O(a,b,c));
XTOSTRUCT_CONDITION() { // load only this return true
int a = -1;
if (obj.has("a")) {
obj["a"].convert(a);
}
return a==2;
}
};
struct Test {
Item arr;
XTOSTRUCT(O(arr));
};
int main(int argc, char *argv) {
string jstr = "{"arr":[{"a":1, "b":11, "c":111},{"a":2, "b":22, "c":222},{"a":3, "b":33, "c":333}]}";
Test t;
x2struct::X::loadjson(jstr, t, false);
cout<<t.arr.b<<','<<t.arr.c<<endl;
}
output is:
22,222
answered Nov 19 '18 at 9:27
xyz347xyz347
613
613
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341987%2fc-json-get-object-members-in-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown