c++, JSON, get object members in an array
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 at 16:36
Amir-Mousavi
598828
598828
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
It's a JSON array, you need to iterate over it. So your approach is the simple and direct one.
add a comment |
up vote
0
down vote
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 |
up vote
0
down vote
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 |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
It's a JSON array, you need to iterate over it. So your approach is the simple and direct one.
add a comment |
up vote
1
down vote
It's a JSON array, you need to iterate over it. So your approach is the simple and direct one.
add a comment |
up vote
1
down vote
up vote
1
down vote
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 yesterday
rioki
3,92942247
3,92942247
add a comment |
add a comment |
up vote
0
down vote
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 |
up vote
0
down vote
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 |
up vote
0
down vote
up vote
0
down vote
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 yesterday
xyz347
263
263
add a comment |
add a comment |
up vote
0
down vote
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 |
up vote
0
down vote
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 |
up vote
0
down vote
up vote
0
down vote
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 yesterday
Caleth
14.7k11736
14.7k11736
add a comment |
add a comment |
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