『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 151|回复: 0

[求助] esphome mqtt json问题求助

[复制链接]

25

主题

224

帖子

1019

积分

金牌会员

007潜水了

Rank: 6Rank: 6

积分
1019
金钱
795
HASS币
0
发表于 2024-3-24 19:14:28 | 显示全部楼层 |阅读模式
esphome mqtt 消息的json处理数组数据用于 remote_transmitter.transmit_raw

处理数组报错了
mqtt:
  broker: -
  on_json_message:
    - topic: transmit_raw
      then:
        - remote_transmitter.transmit_raw:
            code: !lambda |-
              std::vector<int> code;
              JsonArray arr = x["code"];
              for (JsonVariant item : arr) {
                int c = item.as<int>();
                code.push_back(c);
              }

              return code;


In file included from .piolibdeps/lifesmart3/ArduinoJson/src/ArduinoJson/Array/ArrayIterator.hpp:8,
                 from .piolibdeps/lifesmart3/ArduinoJson/src/ArduinoJson/Array/ArrayRef.hpp:8,
                 from .piolibdeps/lifesmart3/ArduinoJson/src/ArduinoJson.hpp:17,
                 from .piolibdeps/lifesmart3/ArduinoJson/src/ArduinoJson.h:9,
                 from src/esphome/components/json/json_util.h:11,
                 from src/esphome/core/string_ref.h:10,
                 from src/esphome/core/entity_base.h:5,
                 from src/esphome/components/binary_sensor/binary_sensor.h:4,
                 from src/esphome/components/binary_sensor/automation.h:9,
                 from src/esphome.h:3,
                 from src/main.cpp:3:
.piolibdeps/lifesmart3/ArduinoJson/src/ArduinoJson/Variant/VariantRef.hpp: In instantiation of 'typename ArduinoJson6185_D1::enable_if<((! ArduinoJson6185_D1::is_same<T, char*>::value) && (! ArduinoJson6185_D1::is_same<T, char>::value)), T>::type ArduinoJson6185_D1::VariantConstRef::as() const [with T = ArduinoJson6185_D1::ArrayRef; typename ArduinoJson6185_D1::enable_if<((! ArduinoJson6185_D1::is_same<T, char*>::value) && (! ArduinoJson6185_D1::is_same<T, char>::value)), T>::type = ArduinoJson6185_D1::ArrayRef]':
.piolibdeps/lifesmart3/ArduinoJson/src/ArduinoJson/Variant/VariantRef.hpp:296:17:   required from 'ArduinoJson6185_D1::VariantConstRef::operator T() const [with T = ArduinoJson6185_D1::ArrayRef]'
/config/lifesmart3.yaml:36:31:   required from here
.piolibdeps/lifesmart3/ArduinoJson/src/ArduinoJson/Variant/VariantRef.hpp:253:34: error: invalid use of incomplete type 'class ArduinoJson6185_D1::InvalidConversion<ArduinoJson6185_D1::VariantConstRef, ArduinoJson6185_D1::ArrayRef>'
  253 |     return Converter<T>::fromJson(*this);
      |            ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
In file included from .piolibdeps/lifesmart3/ArduinoJson/src/ArduinoJson/Variant/VariantRef.hpp:14,
                 from .piolibdeps/lifesmart3/ArduinoJson/src/ArduinoJson/Array/ArrayIterator.hpp:8,
                 from .piolibdeps/lifesmart3/ArduinoJson/src/ArduinoJson/Array/ArrayRef.hpp:8,
                 from .piolibdeps/lifesmart3/ArduinoJson/src/ArduinoJson.hpp:17,
                 from .piolibdeps/lifesmart3/ArduinoJson/src/ArduinoJson.h:9,
                 from src/esphome/components/json/json_util.h:11,
                 from src/esphome/core/string_ref.h:10,
                 from src/esphome/core/entity_base.h:5,
                 from src/esphome/components/binary_sensor/binary_sensor.h:4,
                 from src/esphome/components/binary_sensor/automation.h:9,
                 from src/esphome.h:3,
                 from src/main.cpp:3:
.piolibdeps/lifesmart3/ArduinoJson/src/ArduinoJson/Variant/Converter.hpp:14:7: note: declaration of 'class ArduinoJson6185_D1::InvalidConversion<ArduinoJson6185_D1::VariantConstRef, ArduinoJson6185_D1::ArrayRef>'
   14 | class InvalidConversion;  // Error here? See https://arduinojson.org/v6/invalid-conversion/
      |       ^~~~~~~~~~~~~~~~~
*** [.pioenvs/lifesmart3/src/main.cpp.o] Error 1
在arduinojson的例子里测了下语法感觉没问题

// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
//
// This example shows how to deserialize a JSON document with ArduinoJson.

#include <iostream>
#include "ArduinoJson.h"
#include <vector>
using namespace std;

int main() {
  // Allocate the JSON document
  JsonDocument doc;

  // JSON input string
  const char* json =
      "{"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}";

  // Deserialize the JSON document
  DeserializationError error = deserializeJson(doc, json);

  // Test if parsing succeeds
  if (error) {
    std::cerr << "deserializeJson() failed: " << error.c_str() << std::endl;
    return 1;
  }

  // Fetch the values
  //
  // Most of the time, you can rely on the implicit casts.
  // In other case, you can do doc["time"].as<long>();
  const char* sensor = doc["sensor"];
  long time = doc["time"];
  double latitude = doc["data"][0];
  double longitude = doc["data"][1];
  std::vector<double> x;
  JsonArray arr = doc["data"];
  for (JsonVariant item: arr) {
      double code = item.as<double>();
      x.push_back(code);
  }
  std::cout << x.size() << std::endl;
  // Print the values
  std::cout << sensor << std::endl;
  std::cout << time << std::endl;
  std::cout << latitude << std::endl;
  std::cout << longitude << std::endl;

  return 0;
}


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-6-3 02:29 , Processed in 0.075161 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表