读取.yaml文件和json文件

yaml文件:

	auto label_cls = YAML::LoadFile("label.yaml");
    std::vector<std::string> m_label_cls;
    if (label_cls["label"])
    {
        m_label_cls = label_cls["label"].as<std::vector<std::string>>();
    } 

取为.yaml数据时类型:

 .as<bool>();
 .as<int>();
 .as<std::string>();
 .as<float>();
 .as<std::vector<std::string>>()

if (config["face"]["face_enable"])
            opt.is_face_enable = config["face"]["face_enable"].as<bool>();

写json报文并发出去

 	nlohmann::json js_response;
    js_response["EventType"] = "app_task_config";
    js_response["Result"]["code"] = 0;
    js_response["Result"]["Desc"] = "";
    SimpleWeb::CaseInsensitiveMultimap header;
    header.emplace("Content-Type", "application/json");
    response->write(js_response.dump(), header);

解析json报文

 auto& taskId = req_js["TaskId"].get_ref<const std::string&>(); //引用
 auto command = req_js["Command"].get<int>();

auto& SourceAndRule = req_js["SourceAndRule"];

 for (auto Item: SourceAndRule["Item"])
{
  auto& termId = Item["TermId"].get_ref<const std::string&>();
}

if (req_js.find("Type") != req_js.end())
{
  cam_it->m_urlType = req_js["Type"].get<int>(); // 0 for realplay, 1 for replay
  taskInfo.m_urlType = req_js["Type"].get<int>();
}


 if(RuleProperty["Point"].empty()) //判空
for (auto& Point: RuleProperty["Point"])
{
    cvPoint = cv::Point(0.0);
    if (Point.find("x") != Point.end())
        cvPoint.x = Point["x"].get<int>();
    if (Point.find("y") != Point.end())
        cvPoint.y = Point["y"].get<int>();
    cvPointVec.push_back(cvPoint);
}

json转string js_response.dump()
string转json格式
std::string taskInfo = task->m_taskInfo;
nlohmann::json req_js = nlohmann::json::parse(taskInfo);;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值