Arduino 处理JSON格式的数据


Arduino 处理JSON格式的数据

 

1、安装 ArduinoJson这个包

2、程序代码

# include <ArduinoJson.h>

#define ALINK_BODY_FORMAT "{\"id\":\"123\",\"version123\":\"1.0\",\"method\":\"thing.event.property.post\",\"params\":%s}"

void setup() {
  // 将 ALINK_BODY_FORMAT 替换成你想处理的字符串
  DynamicJsonDocument doc(1024);
  deserializeJson(doc, ALINK_BODY_FORMAT);
  JsonObject obj = doc.as<JsonObject>();
  
  Serial.begin(115200);
  String my_method = obj["method"];
  // my_method = obj["method"].as<String>();
  Serial.println(my_method);

  String no_param = obj["no"];
  Serial.println(no_param);
  if (no_param == "null") {
    Serial.println("NO PARAM");
  }
  if (no_param != "null") {
    Serial.println("NOT NO PARAM");
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM