C++ 在Arduino上解析Bluetooth.write json字符串

C++ 在Arduino上解析Bluetooth.write json字符串,c++,json,arduino,C++,Json,Arduino,我正试图解析从我的手机传入Arduino的JSON字符串。我对C++和这些库比较陌生,所以我有一些问题。下面是我到目前为止的代码。除了我试图将json[]设置为来自手机的值的部分之外,其他部分都可以正常工作。它是一个有效的JSON,并返回到如下所示的控制台 错误:初始值设定项无法确定“json”的大小 从手机接收到JSON: {"SSID":"Sample","PSK":"test","TOKEN":"1234","HOSTNAME":"secondarycontroller"} ESP32代

我正试图解析从我的手机传入Arduino的JSON字符串。我对C++和这些库比较陌生,所以我有一些问题。下面是我到目前为止的代码。除了我试图将json[]设置为来自手机的值的部分之外,其他部分都可以正常工作。它是一个有效的JSON,并返回到如下所示的控制台


错误:初始值设定项无法确定“json”的大小

从手机接收到JSON:

{"SSID":"Sample","PSK":"test","TOKEN":"1234","HOSTNAME":"secondarycontroller"}
ESP32代码:

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <ArduinoJson.h>

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

class MyCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharacteristic) {
      std::string value = pCharacteristic->getValue();


      if(value.length() > 0) {

        StaticJsonDocument<800> doc;
        char json[] = value;
        DeserializationError error = deserializeJson(doc, json);
        if (error) {
          Serial.print(F("deserializeJson() failed: "));
          Serial.println(error.c_str());
          return;
        }

        Serial.println(doc["SSID"]);
        Serial.println(doc["PSK"]);
        Serial.println(doc["HOSTNAME"]);
        Serial.println(doc["TOKEN"]);


        Serial.println("*********");
        Serial.print("New value: ");
        for (int i = 0; i < value.length(); i++)
          Serial.print(value[i]);

        Serial.println();
        Serial.println("*********");
      }
    }
};

void setup() {
  Serial.begin(115200);
  pinMode(LEDpin, OUTPUT);

  Serial.println("1- Download and install an BLE scanner app in your phone");
  Serial.println("2- Scan for BLE devices in the app");
  Serial.println("3- Connect to MyESP32");
  Serial.println("4- Go to CUSTOM CHARACTERISTIC in CUSTOM SERVICE and write something");
  Serial.println("5- See the magic =)");

  BLEDevice::init("MyESP32");
  BLEServer *pServer = BLEDevice::createServer();

  BLEService *pService = pServer->createService(SERVICE_UUID);

  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setCallbacks(new MyCallbacks());

  pCharacteristic->setValue("Hello World");
  pService->start();

  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->start();


}

void loop() {
  // put your main code here, to run repeatedly:
  delay(2000);
}
#包括
#包括
#包括
#包括
#定义服务标识“4fafc201-1fb5-459e-8fcc-c5c9c331914b”
#定义特性_UUID“beb5483e-36e1-4688-b7f5-ea07361b26a8”
类MyCallbacks:public BLECharacteristicCallbacks{
写入时无效(BLECharacteristic*pCharacteristic){
std::string value=pCharacteristic->getValue();
如果(value.length()>0){
静态文件;
char json[]=值;
反序列化错误=反序列化json(doc,json);
如果(错误){
print(F(“反序列化JSON()失败:”);
Serial.println(error.c_str());
回来
}
Serial.println(doc[“SSID”]);
序列号.println(doc[“PSK”]);
Serial.println(doc[“HOSTNAME”]);
Serial.println(doc[“TOKEN”]);
Serial.println(“**********”);
Serial.print(“新值:”);
对于(int i=0;icreateService(服务\u UUID);
BLECharacteristic*pCharacteristic=pService->createCharacteristic(
特点,
BLECharacteristic::属性\读取|
BLECharacteristic::属性\写入
);
pCharacteristic->setCallbacks(newmycallbacks());
pCharacteristic->setValue(“你好世界”);
pService->start();
BLEAdvertising*pAdvertising=pServer->getAdvertising();
pAdvertising->start();
}
void循环(){
//将主代码放在此处,以便重复运行:
延迟(2000年);
}
根据,第二个输入参数接受零拷贝的
char*
;并且,
const char*
const std::string&
具有重复

文件中的原型(仅相关原型):

观察上述原型中的第二个参数


因此,以下几行:

char json[] = value;
DeserializationError error = deserializeJson(doc, json);
将是:

const char* json = value.c_str();
DeserializationError error = deserializeJson(doc, json);
或者,像这样直接传递

DeserializationError error = deserializeJson(doc, value);
那应该能解决你的问题


您得到的错误是关于这一行的:

char json[] = value;
//       ^^
此处缺少
char
数组的大小。必须在此上下文中指定静态数组的大小。另外,在这种情况下,
不能直接转换


提示:

在以下情况下使用:

if(value.length() > 0)
i、 e


它更具可读性和表现力

您得到了什么错误?初始化器无法确定“json”的大小抱歉,不熟悉这个库的实际工作情况-我倾向于将这些json作为文本字符串读入,然后去掉我要查找的位。不美观,但它可以工作。我正在接近自己创建一个函数,因为你这样做。这修复了值问题,但文档不返回其值。Serial.println(doc[“SSID”]);导致以下错误。重载的“println(ArduinoJson6141_u0000010::enable_if::type)”的调用为ambiguous@James:这将解决您的问题:。您应该尽可能多地参考文档,尤其是在使用任何API之前。那会加快你的发展。
if(value.length() > 0)
if( !value.empty() )