NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum

I can receive the JSON result successfully and printing it to the console (as per the online samples), but I am have problems extracting actual field data. How can I extract individual fields as numbers, string, etc?

I've tried various forms of casting, but this is what I have at the moment (trying to extract the status value):

const int gr_result_code = routing_machine.RunQuery(route_parameters, json_result);
std::string sStat("status");
auto it = json_result.values.find(sStat);
osrm::json::Number vv =  (osrm::json::Number) ((*it).second); // doesn't compile
int v = (int) (vv.value); // probably some dodgy rounding here

The Number casting is producing compiler errors. I guess I could convert the object to a string, and then using a third party JSON parser to extract individual fields, but this seems very wasteful.

asked 17 Sep '15, 18:19

winwaed's gravatar image

winwaed
66125
accept rate: 100%


With help from Daniel Hofman on the OSM listserver, I've managed to find the mapbox::util::get call, eg:

    std::string sStat("status");
    auto it = json_result.values.find(sStat);
    double v = mapbox::util::get<osrm::json::Number>( (it->second) ).value;

    SimpleLogger().Write() << " status: " << v;

(in this example, status only contains integer values, so it should also be rounded to an integer)

I think I need to read up on boost :-)

permanent link

answered 21 Sep '15, 17:53

winwaed's gravatar image

winwaed
66125
accept rate: 100%

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×85
×28
×10

question asked: 17 Sep '15, 18:19

question was seen: 4,214 times

last updated: 21 Sep '15, 17:53

NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum