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