c++ - 如何从json文件读取值数组到c++数组
问题描述
我有一个带有值和一组值的 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 数组。
解决方案
您可以通过以下方式编写。
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();
}
}
推荐阅读
- javascript - 如何通过两个 HTML 文件共享一个数组
- c# - 在容器中找不到要调试的 Docker 程序
- java - 如何从 curl 或 postman 或 swagger Micronaut 传递值 @Body MultipartBody
- reactjs - 表单提交计数
- maxima - Maxima:自定义输出显示时如何向用户保留提示信息(例如 asksign())?
- c++ - 调用sizeof()时,00('\0')后面多了一个字节输出,sizeof()的输出比strlen()多2
- json - DB::table Select() 以字符串形式返回数组 Laravel
- javascript - 命中新api后如何使实时图表恢复正常
- javascript - 如何从猫鼬模式拼接项目然后保存文档或模式?
- c# - EF Core 不会根据关系属性进行更新