本站改版新增arduino频道
arduino 请求json 解析json
#include#include#includeconst char* ssid = "NBWIFI2"; const char* password = "z7758521"; void setup() { Serial.begin(115200); // 连接 WiFi WiFi.begin(ssid, password); Serial.print("正在连接 Wi-Fi"); // 检测是否连接成功 while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("连接成功"); Serial.print("IP 地址:"); Serial.println(WiFi.localIP()); } void loop() { if (WiFi.status() == WL_CONNECTED) { HTTPClient http; http.begin("https://restapi.amap.com/v3/weather/weatherInfo?city=141081&key=e43f5609a249f5e7075b5bee8a73bb7a"); // Specify the URL int httpCode = http.GET(); // Send the request if (httpCode > 0) { // Check the returning code String payload = http.getString(); // Get the request response payload // JSON 数据 String json = payload; // 创建 JsonDocument 对象 StaticJsonDocumentdoc; // 解析 JSON 数据 DeserializationError error = deserializeJson(doc, json); if (error) { Serial.print(F("deserializeJson() failed: ")); Serial.println(error.f_str()); return; } // 提取值 const char* status = doc["status"]; const char* count = doc["count"]; const char* info = doc["info"]; const char* infocode = doc["infocode"]; const char* province = doc["lives"][0]["province"]; const char* city = doc["lives"][0]["city"]; const char* adcode = doc["lives"][0]["adcode"]; const char* weather = doc["lives"][0]["weather"]; const char* temperature = doc["lives"][0]["temperature"]; const char* winddirection = doc["lives"][0]["winddirection"]; const char* windpower = doc["lives"][0]["windpower"]; const char* humidity = doc["lives"][0]["humidity"]; const char* reporttime = doc["lives"][0]["reporttime"]; const char* temperature_float = doc["lives"][0]["temperature_float"]; const char* humidity_float = doc["lives"][0]["humidity_float"]; // 打印值 Serial.println(status); Serial.println(count); Serial.println(info); Serial.println(infocode); Serial.println(province); Serial.println(city); Serial.println(adcode); Serial.println(weather); Serial.println(temperature); Serial.println(winddirection); Serial.println(windpower); Serial.println(humidity); Serial.println(reporttime); Serial.println(temperature_float); Serial.println(humidity_float); } http.end(); // Close connection } delay(10000); // Send a request every 10 seconds }
Copyright © 2014 ESP56.com All Rights Reserved
晋ICP备14006235号-22 晋公网安备14108102001165号
执行时间: 0.0095860958099365 seconds