Reading the Whole Array of Data from JSON
-
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
-
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
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
-
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
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
-
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
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
-
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
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"?
-
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"?
hi m_data is showing blank and m_children is showing an address.
-
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"?
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<