ArduinoJson用法实战

C++平台下,ArduinoJson 功能强大,调用简单、简洁,远非jsoncpp之流可比。

调用就一个头文件即可。

include <ArduinoJson.hpp>

关于它的使用还是以代码来记录备忘:

一:序列化为json字符串过程:

bool ECNCapHelper::addSnCodeReadingLog(const std::string msn, bool stateN,
    std::wstring& respoinseStr) {

    DynamicJsonDocument root(8192);
    if (stateN)
    {
        root["sn"] = msn;//  "C00001";    sn码
        root["ncType"] = ""; //成功时为null
    }
    else
    {
        root["sn"] = "NA";//  "C00001";    sn码,
        std::string mMsg = "读码失败";
        root["ncType"] = (mMsg);; //成功时为null    
    }
    root["stationId"] = UTL->ws2s(GSP->LocalStationNumber);//输入
    root["result"] = std::to_string((stateN ? 1 : 0)); //0,1  1表示成功,0为失败
    root["ncCode"] = "0"; //
    root["ncDesc"] = "";
    root["machineNo"] = UTL->ws2s(GSP->LocalStationNumber);
    root["fixtureNo"] = "";
    root["ip"] = UTL->ws2s(GSP->localIP);//要替换成机台本站IP
    std::string mCodeType = "二维码";
    root["Remark"] = (mCodeType);
    char data[8192];
    serializeJson(root, data);
    string jsonStr = data;
   //jsonStr = UTL->EncodeUtf8String(jsonStr);

    string jsonRes = action("addSnCodeReadingLog", jsonStr);
    string keyValue;
    Response_RESULT mResult;
    parseResult(jsonRes, mResult,keyValue);
    respoinseStr = mResult.mMSG;
    return mResult.reponseState();
}

二、字符串解析的过程(支持多重子嵌套解析):

bool ECNCapHelper::parseDATA_ContainerKeyList(const std::string strJ, Resp_ContainerKeyList& mKeyContianer)
{
    if (strJ.empty())return false;
    DynamicJsonDocument root(8192);
    DeserializationError error = deserializeJson(root, strJ);
    if (error)
    {
        return false;
    }
    JsonObject mData = root["DATA"];
    JsonArray mKeyList = mData["containerKeyList"];
    int msize = mKeyList.size();
    if (mKeyList.size() > 0)
    {
        int i = 0;
        JsonObject idKey = mKeyList[i];
        mKeyContianer.mid = (int)idKey["id"];
        mKeyContianer.shard = (int)idKey["shard"];
        mKeyContianer.containerNumber = (const char*)idKey["containerNumber"];

        mKeyContianer.batchNo = (const char*)idKey["batchNo"];
        mKeyContianer.keyName = (const char*)idKey["keyName"];
        mKeyContianer.keyValue = (const char*)idKey["keyValue"];

        mKeyContianer.createName = (const char*)idKey["createName"];
        mKeyContianer.createTime = (const char*)idKey["createTime"];
        mKeyContianer.updateName = (const char*)idKey["updateName"];
        mKeyContianer.updatTime = (const char*)idKey["updateTime"];
        mKeyContianer.matNo = (const char*)idKey["matNo"];
        mKeyContianer.matVersion = (const char*)idKey["matVersion"];

    }

    return false;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海木漄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值