我正在用 C 開發一個程式,該程式從要在網頁中使用的 DLL 回傳資訊。DLL 回傳一個包含資訊的大結構,但只需要我計劃使用https://github.com/nlohmann/json作為 json 回傳的一些欄位,然后回傳到 char*。
這是結構的示例和每個欄位的值的含義(根據檔案pdf)
struct myStruct {
BYTE StatusCode;
BYTE ErrorCode;
DWORD WarningCode[2];
otherStruct SystemInfo[16];
...
}
StatusCode:
0x00 = No Error
0x01 = Error
0x02 = Ready
...
0x05 = Power Off
WarningCode
0x00 0x00 = No warning
0x02 0x01 = Warning Alert
... etc
這是我如何訪問結構的欄位:
GetInfoStatus(&myStatusStruct);
jInfo["error_code"] = myStatusStruct.ErrorCode;
jInfo["status_code"] = myStatusStruct.StatusCode;
jInfo["warning_code"] = myStatusStruct.WarningCode2;
jInfo["is_available_warning_code"] = myStatusStruct.AvailableWarningCode2;
std::string info = jInfo.dump();
return info.c_str();
// My current return char* "json"
// {"available_warning_code":1,"error_code":255,"status_code":4}
但我想要這樣的東西
{"available_warning_code": [0x01, "warning_alert"], "error_code": [0x01, "error_system_fail"], "status_code": [0x04, "low_battery"]}
或類似的,所以我也可以將錯誤代碼回傳到“字串”或“error_message”,指示含義(翻譯),以便我的后端/前端(NodeJS)稍后可以檢測到“low_battery”并對其進行處理,而不是將 0x04 與表匹配以了解 0x04(與其他鍵中的其他 0x04 不同)
我已經檢查了這個解決方案https://stackoverflow.com/a/208003/4620644但仍然不明白是否最適合我的情況以及如何實施它。我有 20 個錯誤代碼、10 個警告代碼、15 個狀態代碼。
uj5u.com熱心網友回復:
您可以創建一個std:pair并在 json 中使用它。但是,在某個地方,您將不得不輸入所有錯誤訊息。
#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include "json.h"
using namespace nlohmann;
std::pair<int, std::string> make_error(int error)
{
// Use a vector if error codes are sequential
// Otherwise, maybe a switch
std::vector<std::string> error_msgs = {
"No Error", "Error", "Ready"
};
if (error >= 0 && error < error_msgs.size()) {
return std::make_pair(error, error_msgs[error]);
}
else {
return std::make_pair(error, "Unknown");
}
}
int main()
{
json jInfo;
jInfo["error_code"] = make_error(2);
std::cout << jInfo.dump();
return 0;
}
這輸出:
{"error_code":[2,"Ready"]}
您也必須對其他欄位執行此操作。
uj5u.com熱心網友回復:
獲取錯誤字串
class CodeMap {
map<pair<int, int>, string> m_warningCodes {
{make_pair(0,0), "No warning"},
{make_pair(2,1), "Warning Alert"}
};
map<int, string> m_statusCode{
{0, "No Error"},
{1, "Error"},
{2, "Ready"},
{5, "Power Off"},
};
public:
std::string GetWarningCode(int code[]){
return m_warningCodes[make_pair(code[0], code[1])];
}
std::string GetStatusCode(int code){
return m_statusCode[code];
}
};
json 中的十六進制不符合 RFC 7159
https://github.com/nlohmann/json/issues/249
方法 1:十六進制字串
從 int 型別獲取十六進制
//with c 20 std::format can be used instead below function
std::string GetHex(int i) {
std::stringstream stream;
stream << "0x" <<std::hex << i;
return stream.str();
}
將鍵值對分配給 json 欄位
CodeMap m; //To get message string
jInfo["error_code"] = make_pair(GetHex(myStatusStruct.ErrorCode), "error_system_fail");
jInfo["status_code"] = make_pair(GetHex(myStatusStruct.StatusCode), m.GetStatusCode(myStatusStruct.StatusCode));
輸出:
"error_code": ["0x01", "error_system_fail"], "status_code": ["0x04", "low_battery"]
方法 2:十六進制為整數
將鍵值對分配給 json 欄位
CodeMap m; //To get message string
jInfo["error_code"] = make_pair(myStatusStruct.ErrorCode, "error_system_fail");
jInfo["status_code"] = make_pair(myStatusStruct.StatusCode, m.GetStatusCode(myStatusStruct.StatusCode));
輸出:
"error_code": [1, "error_system_fail"], "status_code": [4, "low_battery"]
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/367352.html
上一篇:如何從mongodbgrep值
