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