1

How to convert JSON String for example

{"f0":100,"f1":100,"f2":100}

It is what I get from my esp01 side from I2C communication

Now I want it to get it back to my Arduino side variables int f0 ,int f1 ,int f2

But example shown in web site uses kind of different serialized JSON

Example shown,

char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";

Can I convert the data I got from I2C communication for using in the method for deserializeJson(jsonobj, stringofdata)

I2C communication side of arduino looks like,

void receiveEvent(size_t howMany) {
  String datain = "";
  while (0 < Wire.available()) {
    char c = Wire.read();
    datain += c;
  }
  Serial.println(datain);
//be careful can't send too much json data I2C crash!
  Serial.println(datain.length());
  DynamicJsonDocument datajson(100);
  if (deserializeJson(datajson, datain)) {
    Serial.print("deserialization ok ");
    digitalWrite(13, HIGH);
    delay(250);
    digitalWrite(13, LOW);
    f0 = datajson["f0"];
    f1 = datajson["f1"];
    f2 = datajson["f2"];

    Serial.println(String(f0) + " " + String(f1) + " " + String(f2));
  }


}

In serial monitor I only see this no deserialization worked

{"f0":0,"f1":0,"f2":0}
22
5
  • 1
    the return value of deserializeJson is not boolean.
    – Juraj
    Feb 21 at 19:03
  • You cannot concatenate string literals (" ") with +. That just adds the pointer addresses together.
    – Majenko
    Feb 21 at 19:07
  • I tried with DeserializationError error error inside if condition still no luck
    – Avon97
    Feb 21 at 19:32
  • Debug errors like that: arduino if (err) { Serial.print(F("deserializeJson() failed: ")) Serial.println(err.c_str()) } else { //your code } Feb 21 at 20:15
  • @Juraj I believe you almost answered my question it is not a Boolean but however it works as Boolean however for a correct deserialization I need to take the inverse of it that, It was my mistake I first thought that gives a Boolean as correct deserialization as true its not;
    – Avon97
    Feb 22 at 4:15

1 Answer 1

2

The mistake I did was thinking deserializeJson(datajson, datain) gives a Boolean as true for correct deserialization but accually it give a object of DeserializationError err so need to check if there is no error thus

New code

// need ! here
  if (!deserializeJson(datajson, datain)) {
    Serial.println("yay it worked");
    Serial.print("deserialization ok ");
    digitalWrite(13, HIGH);
    delay(250);
    digitalWrite(13, LOW);
    f0 = datajson["f0"];
    f1 = datajson["f1"];
    f2 = datajson["f2"];

    Serial.print(f0);
    Serial.print(" ");
    Serial.print(f1);
    Serial.print(" ");
    Serial.println(f2);

//also tested this way and it works too
    Serial.println(String(f0) + " " + String(f1) + " " + String(f2));
  } else {
    // I was coding here thinking its Boolean before
    //error
  }
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.