ESP32 Json not recognising String variables

Hi all
I'm having a problem with ESP32 using ArduinoJson, I think it might be a system problem but i'm really not sure how to trouble shoot it

#include "painlessMesh.h"
#include <Streaming.h>
#include <Arduino_JSON.h>
#include <WiFi.h>
//#include <Preferences.h>
//#include <Scheduler.h>
#include <String.h>
//Preferences nvr;
//------------------------------------------

#define   MESH_PREFIX     "DavesMesh"
#define   MESH_PASSWORD   "DavesMesh"
#define   MESH_PORT       5555
//------------------------------------------
//********************************************
//  Dallas DS18B20 sensor
//********************************************
  #include <OneWire.h>
  #include <DallasTemperature.h>
  OneWire oneWire(32);   // DallasTempPin);
  DallasTemperature sensors(&oneWire);
  // use sensors.begin(); in setup
//===============================================

Scheduler scheduler; // to control your personal task
JSONVar obj;

void setJson() {
  obj["one"] = "One";
  obj["two"] = "Two";
  obj["three"] = "Three";
}

void setup() {
  Serial.begin(115200);
  setJson();
  String fred = "Fred";
  
  fred = obj["two"];
}

void loop() {
  // put your main code here, to run repeatedly:

}

when i verify the sketch it comes up with an error:
ambiguous overload for operator= (operand types are String and JSONVar)
on the line fred = obj["two"];
I have tried this on V1.8.18 and V2.2.1
interestingly in 2.2.1 if I hover over fred a baloon comes up telling me that its an int ?????
I tried deleting and re installing both versions of Arduino but the same thing still happens

any help would be GREATLY APPRECIATED
Thanks
Dave

try

  fred = JSON.stringify(obj["two"]);

Hi J-M-L thanks for coming back to me
thats just confusing, it works but do you have an explanation of why ?
Ive been programming using painlessMesh and using the original type of statement throughout my node sketches with no problems then suddenly it started complaining and throwing its legs in the air
the fact that 2.2.1 came up with the bubble saying it was an int pointed to a system fault in my view, prompting the re install
Do you have any idea why this should suddenly start happening, no libs were updated as far as I know

I don’t use that library so can’t tell really

You would need to look at the code of the library and look at the = operator overload to see what types are supported

OK thanks for the help

May be look at what changed in the library

Hi again, this is getting even more confusing
i thought JSON.stringify("fred") had fixed the problem but it just leads into deeper water:

fred = obj["two"];

then using

fred = JSON.stringify(obj["two");

actually returns "Two" with the quotes around it
as though obj["two"] equaled ""Two""
anyone got any ideas please

Dave

that's the difference between JSON.stringify and just direct access without a type

compile and test this

#include <ArduinoJson.h>
void setup() {
  Serial.begin(115200);
  StaticJsonDocument<200> doc;
  doc["first"] = "John";
  doc["last"] = "Doe";

  // Stringify the while JSON object
  String jsonString;
  serializeJson(doc, jsonString);
  Serial.print("Full, stringified = "); Serial.println(jsonString);

  // Stringify an element
  serializeJson(doc["first"], jsonString);
  Serial.print("First, stringified = "); Serial.println(jsonString);

  // direct access and cast
  const char* firstName = doc["first"].as<const char*>();

  // Print the content of "first" field
  Serial.print("First, as<const char*> = "); Serial.println(firstName);

}

void loop() {}

you should see

Full, stringified = {"first":"John","last":"Doe"}
First, stringified = "John"
First, as<const char*> = John

Hi Jml
Thank you, i think my mistake is that i am using Arduino_Json.h lib
not ArduinoJson.h
There seem to be a lot of places where the lib i'm trying to use either does not work or needs a lot of get-arounds
I think my best option is to swap libs
Thanks for all your help
Dave

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.