Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Reading the Whole Array of Data from JSON

Reading the Whole Array of Data from JSON

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structuresjsonhelpquestion
7 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Litu Sahoo
    wrote on last edited by
    #1

    Hi I want the array of data under items, using this code i am able to get only the value under the first structure of data under items. How can i loop to the second structure of data under items, and further more if present. I am doing this using boost in c++. Can someone help on this. items=root.get_child("Serial"); for (boost::property_tree::ptree::iterator itprop = items.begin(); itprop != items.end(); ++itprop) { for(boost::property_tree::ptree::iterator it1=itprop->second.begin();it1!=itprop->second.end();++it1) { if(it1->first=="Items") { boost::property_tree::ptree& rootschild=it1->second; cout<<"Size is "<second.begin();it3!=it2->second. end();++it3) { std::cout<first<<": "<second.get_value()<<"\n"; } } } { "Header": { "Name": "abcd", "Email": "xyz" }, "Serial": [{ "flavour": "Green", "Color": "9", "code": "103", "Items": [{ "date": "2020/11", "value": "3.5", "serial": "01" }, { "date": "2020/10", "value": "3.4", "serial": "03" }] }] } With Regards

    D 1 Reply Last reply
    0
    • L Litu Sahoo

      Hi I want the array of data under items, using this code i am able to get only the value under the first structure of data under items. How can i loop to the second structure of data under items, and further more if present. I am doing this using boost in c++. Can someone help on this. items=root.get_child("Serial"); for (boost::property_tree::ptree::iterator itprop = items.begin(); itprop != items.end(); ++itprop) { for(boost::property_tree::ptree::iterator it1=itprop->second.begin();it1!=itprop->second.end();++it1) { if(it1->first=="Items") { boost::property_tree::ptree& rootschild=it1->second; cout<<"Size is "<second.begin();it3!=it2->second. end();++it3) { std::cout<first<<": "<second.get_value()<<"\n"; } } } { "Header": { "Name": "abcd", "Email": "xyz" }, "Serial": [{ "flavour": "Green", "Color": "9", "code": "103", "Items": [{ "date": "2020/11", "value": "3.5", "serial": "01" }, { "date": "2020/10", "value": "3.4", "serial": "03" }] }] } With Regards

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      What about something like:

      serial_items = root.get_child("Serial");
      items = serial_items.get_child("Items");
      for (boost::property_tree::ptree::iterator itprop = items.begin(); itprop != items.end(); ++itprop)
      ...

      I don't use boost, nor have I tried to compile it, so this may or may not work.

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      L 1 Reply Last reply
      0
      • D David Crow

        What about something like:

        serial_items = root.get_child("Serial");
        items = serial_items.get_child("Items");
        for (boost::property_tree::ptree::iterator itprop = items.begin(); itprop != items.end(); ++itprop)
        ...

        I don't use boost, nor have I tried to compile it, so this may or may not work.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        L Offline
        L Offline
        Litu Sahoo
        wrote on last edited by
        #3

        Hi, Thank you for the reply, but the code is crashing in the below line.

        David Crow wrote:

        items = serial_items.get_child("Items");

        With Regards

        D V 2 Replies Last reply
        0
        • L Litu Sahoo

          Hi, Thank you for the reply, but the code is crashing in the below line.

          David Crow wrote:

          items = serial_items.get_child("Items");

          With Regards

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Litu Sahoo wrote:

          ...but the code is crashing in the below line.

          What exactly does that mean?

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          1 Reply Last reply
          0
          • L Litu Sahoo

            Hi, Thank you for the reply, but the code is crashing in the below line.

            David Crow wrote:

            items = serial_items.get_child("Items");

            With Regards

            V Offline
            V Offline
            Victor Nijegorodov
            wrote on last edited by
            #5

            Litu Sahoo wrote:

            Thank you for the reply, but the code is crashing in the below line.

            David Crow wrote:

            items = serial_items.get_child("Items");

            Did you debug your code? What is the value of

            serial_items

            before the "crash"?

            L 2 Replies Last reply
            0
            • V Victor Nijegorodov

              Litu Sahoo wrote:

              Thank you for the reply, but the code is crashing in the below line.

              David Crow wrote:

              items = serial_items.get_child("Items");

              Did you debug your code? What is the value of

              serial_items

              before the "crash"?

              L Offline
              L Offline
              Litu Sahoo
              wrote on last edited by
              #6

              hi m_data is showing blank and m_children is showing an address.

              1 Reply Last reply
              0
              • V Victor Nijegorodov

                Litu Sahoo wrote:

                Thank you for the reply, but the code is crashing in the below line.

                David Crow wrote:

                items = serial_items.get_child("Items");

                Did you debug your code? What is the value of

                serial_items

                before the "crash"?

                L Offline
                L Offline
                Litu Sahoo
                wrote on last edited by
                #7

                Hi I modified the program but still not able to get the whole array of data Under Items. Below is the sample code. Please let me know where it is wrong. namespace pt=boost::property_tree; pt::ptree root; pt::ptree items; pt::ptree obsrvtns; pt::ptree obsrvtns1; pt::read_json(filepath,root); obsrvtns=root.get_child("Header"); cout<<"Header size is "<first<<": "<second.get_value()<<"\n"; } items=root.get_child("Serial"); cout<<"Series Size is "<second.size()<<"\n"; for(boost::property_tree::ptree::iterator it1=it->second.begin();it1!=it->second.end();++it1) { cout<first<<": "<second.get_value()<<"\n"; if(it1->first=="Item") { /*obsrvtns1=it1->second.get_child("Items"); cout<<"check \n";*/ //BOOST_FOREACH(boost::property_tree::ptree::value_type &v,root.get_child("Header.Serial.Item")) BOOST_FOREACH(boost::property_tree::ptree::value_type &v,root.get_child("Item")) { cout<

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups