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;
}







0















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





























    0















    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

























      0












      0








      0








      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 '18 at 16:36









      Amir-MousaviAmir-Mousavi

      86111238




      86111238
























          3 Answers
          3






          active

          oldest

          votes


















          1














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






          share|improve this answer































            1














            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































              0














              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
























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


                }
                });














                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









                1














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






                share|improve this answer




























                  1














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






                  share|improve this answer


























                    1












                    1








                    1







                    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 Nov 19 '18 at 9:35









                    riokirioki

                    4,16442448




                    4,16442448

























                        1














                        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




























                          1














                          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


























                            1












                            1








                            1







                            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 Nov 19 '18 at 10:16









                            CalethCaleth

                            18.8k22142




                            18.8k22142























                                0














                                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




























                                  0














                                  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


























                                    0












                                    0








                                    0







                                    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 Nov 19 '18 at 9:27









                                    xyz347xyz347

                                    613




                                    613






























                                        draft saved

                                        draft discarded




















































                                        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.




                                        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

                                        Xamarin.iOS Cant Deploy on Iphone

                                        Glorious Revolution

                                        Dulmage-Mendelsohn matrix decomposition in Python