logo elektroda
logo elektroda
X
logo elektroda

[Solved] Serializing ESP32 Data with ArduinoJson in a For Loop: Key Value Issue

xury 408 4
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 20332007
    xury
    Automation specialist
    I have this function:
    Code: Arduino
    Log in, to see the code
    .
    The intention was to take a value from the array and assign another key named e.g. "CellV_01 etc".
    Code: Arduino
    Log in, to see the code


    But I don't know why the iteration in the for loop doesn't work like this. Instead of the expected consecutive key value entries I have one with bushes.

    When I use println in the same loop the key displays correctly.
    What am I doing wrong?
    Do you have a problem with Arduino? Ask question. Visit our forum Arduino.
  • ADVERTISEMENT
  • Helpful post
    #2 20332091
    khoam
    Level 42  
    The result of the operation:82d9bb59b .
    will certainly not be the string "CellV_0x", where x is another number. Concatenation doesn't work like that. It is different when you write:
    Code: C / C++
    Log in, to see the code
    and so it will be OK.
  • ADVERTISEMENT
  • #3 20332109
    xury
    Automation specialist
    I tried it anyway. Now I don't have my code exactly. I was writing from memory.
    I tried various ways: via std::string and by rewriting to const char* etc.
    Is it even possible to iterate like this in arduinojson? Because maybe it is not possible? And is there another way to do it?
  • ADVERTISEMENT
  • Helpful post
    #4 20332118
    khoam
    Level 42  
    I checked before replying to you :) You are iterating over indices which may be of type const char* , so using name.c_str() is valid, as long as it points to a valid string. But these indexes can also be of type String, so c_str() can be omitted :) .
    Link Link
    How the indexes for the StaticJsonDocument are created i.e. whether in a loop or otherwise does not really matter.
  • #5 20332424
    xury
    Automation specialist
    Surprisingly today I did as you suggested and it works beautifully. Thanks a lot mate khoam . This is not the first time you have helped me.
ADVERTISEMENT