0

I want to use boost to read data from a JSON string that might look something like:

 { "version": 2, "data_type": "alpr_results", "epoch_time": 1534253281728, "img_width": 1920, "img_height": 1080, "processing_time_ms": 271.27728271484, "uuid": "", "error": false, "regions_of_interest": [ { "x": 0, "y": 0, "width": 1920, "height": 1080 } ], "results": [ { "plate": "FRJ7248", "confidence": 94.583724975586, "matches_template": 1, "plate_index": 0, "region": "us-ny", "region_confidence": 88, "processing_time_ms": 94.328330993652, "requested_topn": 25, "coordinates": [ { "x": 1545, "y": 233 }, { "x": 1640, "y": 241 }, { "x": 1635, "y": 284 }, { "x": 1543, "y": 276 } ], "vehicle_region": { "x": 1388, "y": 0, "width": 403, "height": 403 }, "candidates": [ { "plate": "FRJ7248", "confidence": 94.583724975586, "matches_template": 1 }, { "plate": "FRJ724", "confidence": 81.629249572754, "matches_template": 1 }, { "plate": "FR7248", "confidence": 81.344184875488, "matches_template": 0 }, { "plate": "FJ7248", "confidence": 81.199394226074, "matches_template": 0 }, { "plate": "RJ7248", "confidence": 81.19913482666, "matches_template": 0 }, { "plate": "FRJ7246", "confidence": 81.167663574219, "matches_template": 1 }, { "plate": "FRJ7240", "confidence": 81.13761138916, "matches_template": 1 }, { "plate": "FRJ7243", "confidence": 81.127967834473, "matches_template": 1 }, { "plate": "FRJ7245", "confidence": 81.124664306641, "matches_template": 1 }, { "plate": "FRU7248", "confidence": 81.089027404785, "matches_template": 1 }, { "plate": "FBJ7248", "confidence": 81.082328796387, "matches_template": 1 }, { "plate": "FRD7248", "confidence": 81.080703735352, "matches_template": 1 }, { "plate": "ERJ7248", "confidence": 81.068466186523, "matches_template": 1 }, { "plate": "FR724", "confidence": 68.389724731445, "matches_template": 0 }, { "plate": "FJ724", "confidence": 68.244926452637, "matches_template": 0 }, { "plate": "RJ724", "confidence": 68.244659423828, "matches_template": 0 }, { "plate": "FRU724", "confidence": 68.134559631348, "matches_template": 1 }, { "plate": "FBJ724", "confidence": 68.127861022949, "matches_template": 1 }, { "plate": "FRD724", "confidence": 68.126243591309, "matches_template": 1 }, { "plate": "ERJ724", "confidence": 68.113998413086, "matches_template": 1 }, { "plate": "F7248", "confidence": 67.959869384766, "matches_template": 0 }, { "plate": "R7248", "confidence": 67.959594726562, "matches_template": 0 }, { "plate": "FR7246", "confidence": 67.92813873291, "matches_template": 0 }, { "plate": "FR7240", "confidence": 67.898078918457, "matches_template": 0 }, { "plate": "FR7243", "confidence": 67.888442993164, "matches_template": 0 } ] }, { "plate": "T701486C", "confidence": 93.531463623047, "matches_template": 0, "plate_index": 1, "region": "us-ny", "region_confidence": 84, "processing_time_ms": 94.328330993652, "requested_topn": 25, "coordinates": [ { "x": 1005, "y": 407 }, { "x": 1101, "y": 400 }, { "x": 1102, "y": 454 }, { "x": 1007, "y": 462 } ], "vehicle_region": { "x": 782, "y": 69, "width": 543, "height": 543 }, "candidates": [ { "plate": "T701486C", "confidence": 93.531463623047, "matches_template": 0 }, { "plate": "T70T486C", "confidence": 82.387817382812, "matches_template": 0 }, { "plate": "T70486C", "confidence": 82.198287963867, "matches_template": 0 }, { "plate": "T70I486C", "confidence": 82.151023864746, "matches_template": 0 }, { "plate": "701486C", "confidence": 82.046699523926, "matches_template": 0 }, { "plate": "T70146C", "confidence": 81.921669006348, "matches_template": 0 }, { "plate": "7701486C", "confidence": 81.844429016113, "matches_template": 0 }, { "plate": "1701486C", "confidence": 81.822814941406, "matches_template": 0 }, { "plate": "T701466C", "confidence": 81.808143615723, "matches_template": 0 }, { "plate": "70T486C", "confidence": 70.903053283691, "matches_template": 0 }, { "plate": "T70T46C", "confidence": 70.778022766113, "matches_template": 0 }, { "plate": "70486C", "confidence": 70.713523864746, "matches_template": 0 }, { "plate": "770T486C", "confidence": 70.700782775879, "matches_template": 0 }, { "plate": "170T486C", "confidence": 70.679168701172, "matches_template": 0 }, { "plate": "70I486C", "confidence": 70.666259765625, "matches_template": 0 }, { "plate": "T70T466C", "confidence": 70.664497375488, "matches_template": 0 }, { "plate": "T7046C", "confidence": 70.588493347168, "matches_template": 0 }, { "plate": "T70I46C", "confidence": 70.541229248047, "matches_template": 0 }, { "plate": "770486C", "confidence": 70.511253356934, "matches_template": 0 }, { "plate": "170486C", "confidence": 70.489639282227, "matches_template": 0 }, { "plate": "T70466C", "confidence": 70.474967956543, "matches_template": 0 }, { "plate": "770I486C", "confidence": 70.463996887207, "matches_template": 0 }, { "plate": "170I486C", "confidence": 70.442375183105, "matches_template": 0 }, { "plate": "70146C", "confidence": 70.436904907227, "matches_template": 0 }, { "plate": "T70I466C", "confidence": 70.427703857422, "matches_template": 0 } ] } ] } 

All I'm interested in for now is the "plate" in "results", i.e. FRJ7248 and T701486C. So far, I've got this:

int alpr_json_decode(std::string json) { try { std::stringstream ss; ss << json; boost::property_tree::ptree pt; boost::property_tree::read_json(ss,pt); BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("results.plate")) { assert(v.first.empty()); std::cout << v.second.data() << std::endl; } } catch(std::exception const& e) { std::cerr <<e.what() << std::endl; } return -1; } 

And I get No such node (results.plate). Why, what am I doing wrong here?
I've also tried .get_child("results") which returns nothing.

1 Answer 1

1

results is array of objects, so according to reference

JSON arrays are mapped to nodes. Each element is a child node with an empty name.

your json is translated into

results { "" { plate FRJ7248 ... } "" { plate T701486C } } 

you should read all objects of list by pt.get_child("results") and then for each subtree you can call v.second.get<string>("XXX") to read member of object.

Test code:

BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("results")) { cout << v.second.get<string>("plate") << endl; cout << v.second.get<string>("confidence") << endl; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.