Passing strings to functions. Is that my problem?

Well I have a lot of problems but so far Google has not been my friend. I saw lots regarding the pitfalls of strings but not exactly sure what to do to get around it. Basically I'm using the ArduinoJson.h library to decode some json encoded stuff that is polled from my website as in...

    returnString = httpGETRequest(serverPath.c_str());
    JsonObject& root = jsonBuffer.parseObject(returnString);
    Serial.println(returnString);    .
    if (!root.success()) {
        Serial.println("Well this sucks.");

It works seemingly solidly for a couple of hours polling every 15 seconds or so and then it fails. I.e. root.success() = false. (So for now I just reboot the sucker but that has some limitations.) However even on failure the return string, returnString, when printed looks good to me so was not sure why the parse failed. So... I decided I was going to learn me something and wanted to build a json decoder ring for simple jsonencoded strings. So this really has nothing to do with my actual problem but in starting my poor man's json decoder I really can't get past square one. I'm putting together a little function that you pass the return string (returnString in my example) and the field string you are looking for in the return string. So this sketch compiles warning free but doesn't give me the result I hoped for.


String json = "{\"ip\":\"\",\"period\":\"60000\",\"heartbeat\":\"30000\",\"debug\":\"1\",\"forced\":\"0\",\"trig_relay\":\"0\"}";

void setup() {
  String field = "heartbeat";
  int i = json.indexOf(field);  //This works returns 41
  String result = jsonDecode(json, field);

String jsonDecode(String &jsonEncoded, String &field) {
  int i;
  if ( i = jsonEncoded.indexOf(field) == -1) return "Foobar";  // this doesn't return the result I was expecting. returns 0 not 41 or -1???
  Serial.print("Index of "); Serial.print(field); Serial.print(" is "); + Serial.println(i);
  return "XYZZY";

void loop() {

So in this example I was expecting that the field would be found at position 41 which it is in the first instance. But when I try to find the position in a function I get 0 as the position. While I was hoping for 41 I wasn't expecting 0 as it if was in the string starting at position 0. I figured if something was messed up it would show a position of -1; So this is telling me I don't understand strings. This is the out put I get when running that sketch.

14:48:00.056 -> {"ip":"","period":"60000","heartbeat":"30000","debug":"1","forced":"0","trig_relay":"0"}
14:48:00.056 -> 41
14:48:00.056 -> 
14:48:00.056 -> {"ip":"","period":"60000","heartbeat":"30000","debug":"1","forced":"0","trig_relay":"0"}
14:48:00.056 -> heartbeat
14:48:00.056 -> Index of heartbeat is 0
14:48:00.090 -> XYZZY

I know I have bigger problems as I know there is a zero chance likelihood that the json library code is wrong so something else is clobbering me but as this first step it would be nice to know what I have wrong in this little sketch. Thx.

You have an operator precedence issue.

if ( i = jsonEncoded.indexOf(field) == -1)

That statement compares the results of the indexOf function with -1 and assigns that result (which is 0 since 41 does not equal -1) to the variable i.

Either wrap your assignment inside parentheses or (better) move the assignment outside the conditional

int i = jsonEncoded.indexOf(field);
if( i == -1) //...

Ooops. Thanks and yes I feel like an idiot. Turns out I was Googling for the wrong thing! :slight_smile: