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?










share|improve this question


























    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?










    share|improve this question
























      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?










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 at 16:36









      Amir-Mousavi

      598828




      598828
























          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.






          share|improve this answer




























            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





            share|improve this answer




























              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





              share|improve this answer





















                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',
                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
                });


                }
                });














                 

                draft saved


                draft discarded


















                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








                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.






                share|improve this answer

























                  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.






                  share|improve this answer























                    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.






                    share|improve this answer












                    It's a JSON array, you need to iterate over it. So your approach is the simple and direct one.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered yesterday









                    rioki

                    3,92942247




                    3,92942247
























                        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





                        share|improve this answer

























                          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





                          share|improve this answer























                            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





                            share|improve this answer












                            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






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered yesterday









                            xyz347

                            263




                            263






















                                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





                                share|improve this answer

























                                  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





                                  share|improve this answer























                                    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





                                    share|improve this answer












                                    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






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered yesterday









                                    Caleth

                                    14.7k11736




                                    14.7k11736






























                                         

                                        draft saved


                                        draft discarded



















































                                         


                                        draft saved


                                        draft discarded














                                        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





















































                                        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







                                        Popular posts from this blog

                                        Cypress Hill

                                        what are some tips for doing well in the interview? [on hold]

                                        How does a super-power salesman not get shut down for legal reasons?