res.jsonValue["History"]; ...-Go服务器开发" />

ZBLOG

nlohmann::json member; member["Data"] = nlohmann::json::parse(data); if (object.find("power") != object.end()) { // Move the "power" object to the beginning of "History" nlohmann::json& historyArray = asyncResp->res.jsonValue["History"]; ...

如果没有 “power” 字段但有 “Temperature” 字段,那么根据您的代码逻辑,会将 “Temperature” 对象插入到数组的开头,而不是第二个位置。要将其放在第二个位置,您可以做一些调整:

nlohmann::json member;
member["Data"] = nlohmann::json::parse(data);

if (object.find("power") != object.end())
{
    // Move the "power" object to the beginning of "History"
    nlohmann::json& historyArray = asyncResp->res.jsonValue["History"];
    historyArray.insert(historyArray.begin(), member);
}
else if (object.find("Temperature") != object.end())
{
    // Insert "Temperature" object at the second position in "History",
    // after the first element
    nlohmann::json& historyArray = asyncResp->res.jsonValue["History"];
    
    if (historyArray.size() > 0)
        historyArray.insert(historyArray.begin() + 1, member);
    else
        historyArray.push_back(member); // If History is empty, add it as the first element
}
else
{
    // Append other objects to the end of "History"
    asyncResp->res.jsonValue["History"].push_back(member);
}

通过上述修改,即使没有 “power” 字段,也能将 “Temperature” 对象插入到数组的第二个位置。如果数组为空,则直接作为第一个元素添加到末尾。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:res.jsonValue["History"]; ...">https://golang.0voice.com/?id=16373

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?