您可以根据您的需求进行以下修改:
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"
nlohmann::json& historyArray = asyncResp->res.jsonValue["History"];
historyArray.insert(historyArray.begin() + 1, member);
}
else
{
// Append other objects to the end of "History"
asyncResp->res.jsonValue["History"].push_back(member);
}
上述代码中,我们首先判断是否存在 “power” 字段,如果存在则将其移到数组的开头。接着判断是否存在 “Temperature” 字段,如果存在则插入到数组的第二个位置。最后,如果既没有 “power” 字段也没有 “Temperature” 字段,则直接将对象追加到数组的末尾。
内容由零声教学AI助手提供,问题来源于学员提问




