首页 > 其他分享 >arduino 之 json 再测试(key是否存在)

arduino 之 json 再测试(key是否存在)

时间:2023-05-09 10:26:26浏览次数:31  
标签:10 set val arduino sjdoc json key hlf Serial

1.使用containsKey():

String do_back(String json) {
StaticJsonDocument<200> sjdoc;
deserializeJson(sjdoc, json);
String val=sjdoc["hlf"].as<String>();
Serial.print("val:"); Serial.println(val);
Serial.print("sjdoc.containsKey(”hlf“):"); Serial.println(sjdoc.containsKey("hlf"));
Serial.print("sjdoc.containsKey(”hlf_set“):"); Serial.println(sjdoc.containsKey("hlf_set"));
Serial.print("sjdoc[”hlf_set“].containsKey(”ctl“):"); Serial.println(sjdoc["hlf_set"].containsKey("ctl"));

if(val=="?"){
Serial.println("val=?:y");

}
return "";
}

 

串口输出结果:

09:37:08.785 -> Message:{"hlf":"?"}
09:37:08.785 -> -----------------------
09:37:08.785 -> val:?
09:37:08.785 -> sjdoc.containsKey(”hlf“):1
09:37:08.785 -> sjdoc.containsKey(”hlf_set“):0
09:37:08.785 -> sjdoc[”hlf_set“].containsKey(”ctl“):0
09:37:08.785 -> val=?:y
 

09:37:26.268 -> Message:{"hlf_set":"?"}
09:37:26.268 -> -----------------------
09:37:26.268 -> val:null
09:37:26.268 -> sjdoc.containsKey(”hlf“):0
09:37:26.268 -> sjdoc.containsKey(”hlf_set“):1
09:37:26.268 -> sjdoc[”hlf_set“].containsKey(”ctl“):0
 

09:38:35.055 -> Message:{"hlf_set":{"ctl":"0","max":"25.2"}}
09:38:35.055 -> -----------------------
09:38:35.055 -> val:null
09:38:35.055 -> sjdoc.containsKey(”hlf“):0
09:38:35.055 -> sjdoc.containsKey(”hlf_set“):1
09:38:35.055 -> sjdoc[”hlf_set“].containsKey(”ctl“):1

09:39:07.613 -> Message:{"hlf_set":{"ct":"0","max":"25.2"}}
09:39:07.613 -> -----------------------
09:39:07.613 -> val:null
09:39:07.613 -> sjdoc.containsKey(”hlf“):0
09:39:07.613 -> sjdoc.containsKey(”hlf_set“):1
09:39:07.613 -> sjdoc[”hlf_set“].containsKey(”ctl“):0

 

2.利用null值

String do_back(String json) {
  StaticJsonDocument<200> sjdoc;
  deserializeJson(sjdoc, json);
  String val=sjdoc["hlf"].as<String>();
  Serial.print("val:");  Serial.println(val);
const char* hlf_set =  sjdoc[”hlf_set“];
const char* hlf_set_ctl =  sjdoc[”hlf_set“]["ctl"];
const char* hlf_set_max =  sjdoc[”hlf_set“]["max"];

if(hlf_set){
  Serial.print(hlf_set==ture:");  Serial.println(hlf_set);
}else{
  Serial.print(hlf_set==f:");  Serial.println(hlf_set);
}
if(hlf_set_ctl){
  Serial.print(hlf_set_ctl==ture:");  Serial.println(hlf_set_ctl);
}else{
  Serial.print(hlf_se_ctlt==f:");  Serial.println(hlf_set_ctl);
}
if(hlf_set_max){
  Serial.print(hlf_set_max==ture:");  Serial.println(hlf_set_max);
}else{
  Serial.print(hlf_se_max==f:");  Serial.println(hlf_set_max);
}
  if(val=="?"){
      Serial.println("val=?:y");
      
  }
 return "";
}

串口输出结果:

 

10:09:38.664 -> Message:{"hlf_set":{"ct":"0","max":"25.2"}}
10:09:38.664 -> -----------------------
10:09:38.664 -> val:null
10:09:38.698 -> hlf_set==f:
10:09:38.698 -> hlf_se_ctlt==f:
10:09:38.698 -> hlf_set_max==ture:25.2

10:10:33.065 -> Message:{"hlf_set":{"ctl":"0","min":"25.2"}}
10:10:33.065 -> -----------------------
10:10:33.100 -> val:null
10:10:33.100 -> hlf_set==f:
10:10:33.100 -> hlf_set_ctl==ture:0
10:10:33.100 -> hlf_se_max==f:

10:11:21.062 -> Message:{"hlf_set":"15"}
10:11:21.062 -> -----------------------
10:11:21.062 -> val:null
10:11:21.062 -> hlf_set==ture:15
10:11:21.062 -> hlf_se_ctlt==f:
10:11:21.062 -> hlf_se_max==f:

10:12:24.234 -> Message:{"hlf_set":{"ctl":"0","max":"25.2","min":"15.2"}}
10:12:24.268 -> -----------------------
10:12:24.268 -> val:null
10:12:24.268 -> hlf_set==f:
10:12:24.268 -> hlf_set_ctl==ture:0
10:12:24.268 -> hlf_set_max==ture:25.2

 

标签:10,set,val,arduino,sjdoc,json,key,hlf,Serial
From: https://www.cnblogs.com/chengwh/p/17384068.html

相关文章

  • Keycloak: Requesting Token with Password Grant
    Keycloak:RequestingTokenwithPasswordGranthttps://www.appsdeveloperblog.com/keycloak-requesting-token-with-password-grant/Inthistutorial,youwilllearnhowtouseaPasswordGrantOAuth2authorizationflowtorequestanAccessTokenandaRefre......
  • Keycloak: Authorization Code Grant Example
    Keycloak:AuthorizationCodeGrantExamplehttps://www.appsdeveloperblog.com/keycloak-authorization-code-grant-example/ 适合web应用 Inthistutorial,youwilllearnhowtogetanaccesstokenfromtheKeycloakauthorizationserverusingtheOAuthAuthor......
  • 获取.Net程序集的PublicKeyToken
    C:\ProgramData\Microsoft\Windows\StartMenu\Programs\VisualStudio2022\VisualStudioToolsSN-TC:\Users\97627\.nuget\packages\system.data.sqlclient\4.8.5\ref\netcoreapp2.1\System.Data.SqlClient.dll#SN-TYou.dll   ......
  • golang map key struct hash policy
     Theeasiestandmostflexiblewayistousea struct asthekeytype,includingallthedatayouwanttobepartofthekey,soinyourcase:typeKeystruct{X,Yint}Andthat'sall.Usingit:m:=map[Key]int{}m[Key{2,2}]=4m[Key{2......
  • QT5 QJson与QString相互转换
    QT5QJson与QString相互转换QString转QJsonQJsonObjectMainWindow::QstringToJson(QStringjsonString){QJsonDocumentjsonDocument=QJsonDocument::fromJson(jsonString.toLocal8Bit().data());if(jsonDocument.isNull()){qDebug()<<"StringNULL&......
  • GSON -- convert java object to json
    Library:gson(2.2.4) <dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.2.4</version></dependency>  (1)SimpleJava“POJO”/JSON packagecom.hmkco......
  • java获取json字符串中json对象
    StringruleDetail=paperRule.getRuleDetail();if(ruleDetail!=null){JSONObjectjsonObject=JSONObject.fromObject(ruleDetail);//转json对象ObjectpaperRules=jsonObject.get("paper......
  • C# .net core 返回json 中文字符编码被转换或乱码问题
    开发环境VS2022+.NET6.0现象接口返回Json中文数据时出现乱码。例如后台返回结果:"0506133015\u56FE\u8868\u9009\u62E9.png"。解决办法以下方法任选其一即可。//方法1:在Program.cs中添加以下代码varbuilder=WebApplication.CreateBuilder(args);builder.Services.Ad......
  • 用reids查询出特定key开头的key的数量
    redis查询key的命令只有:DBSIZE,查询出特定key开头的key数量是没有的。但是可以使用lua脚本。如果我想查询出以ids开头的数量,可以这样写:方式一:在命令上直接写EVAL"localkeys=redis.call('keys','ids*')returntable.getn(keys)"0方法二:先在文件上写在执行localkeys......
  • Git--no matching host key type found. Their offer: ssh-rsa
     解决方法:在用户目录下的.ssh文件夹下新建一个config文件Host*HostKeyAlgorithms+ssh-rsaPubKeyAcceptedKeyTypes +ssh-rsa......