简体   繁体   中英

How to read an array of values from json file to c++ array

I have a sample.json with values and an array of values. I'm currently using Json library to parse the json file and read the contents into C++ code. I know how to read a value but not sure about reading an array

Below is the content of sample.json file.

"steering_facts" :
{
    "SteerPolynomial": [0.0, 0.0, -0.0006148, 0.025, 16.24, -0.3823],
    "SteerRatio"     : 0.0
}

Here i can read "SteerRatio" with the help of below code.

static Json::Value  jsonValues;
if (jsonValues.isMember("steering_facts")){
    float steerRatio = jsonValues["steering_facts"]["SteerRatio"].asFloat();
}

But not sure how to read SteerPolynomial array.

You can write through the following way.

static Json::Value  jsonValues;
if (jsonValues.isMember("steering_facts")){
    float steerRatio = jsonValues["steering_facts"]["SteerRatio"].asFloat();
    const Json::Value mynames = jsonValues["steering_facts"]["SteerPolynomial"];
    for ( int index = 0; index < mynames.size(); ++index )
    {
        float poli = mynames[index].asFloat();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM