我有一个带有值和值数组的sample.json。 我目前正在使用Json库来解析json文件并将内容读入C ++代码。 我知道如何读取值,但不确定读取数组
下面是sample.json文件的内容。
"steering_facts" :
{
"SteerPolynomial": [0.0, 0.0, -0.0006148, 0.025, 16.24, -0.3823],
"SteerRatio" : 0.0
}
在这里,我可以借助以下代码阅读“ SteerRatio”。
static Json::Value jsonValues;
if (jsonValues.isMember("steering_facts")){
float steerRatio = jsonValues["steering_facts"]["SteerRatio"].asFloat();
}
但不确定如何读取SteerPolynomial数组。
答案 0 :(得分:2)
您可以通过以下方式编写。
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();
}
}