本站改版新增arduino频道
arduino 请求股票api 解析json
#include#include#includeconst char* ssid = "NBWIFI";
const char* password = "z7758521";
String current;
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("http://www.api56.com/api_stock/"); // Specify the URL
    int httpCode = http.GET(); // Send the request
    if (httpCode > 0) { // Check the returning code
      String json = http.getString(); // Get the request response payload
 // 创建 JsonDocument 对象
  StaticJsonDocumentdoc;
  // 解析 JSON 数据
  DeserializationError error = deserializeJson(doc, json);
  // 检查是否有错误
  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.f_str());
    return;
  }
  // 获取并打印 "symbol" 的值
  const char* symbol = doc["data"][0]["symbol"];
  current = doc["data"][0]["current"].as(); 
Serial.println(symbol);
Serial.println(current);
    }
    http.end(); // Close connection
  }
  delay(10000); // Send a request every 10 seconds
}
Copyright © 2014 ESP56.com All Rights Reserved  
晋ICP备14006235号-22 晋公网安备14108102001165号    
执行时间: 0.0093410015106201 seconds