Get data using sscanf()

hey there!
I have the text below:
[{“modemOrderId”:1,“orderType”:“ElecMeter_min”,“order”:“3”}]

what I want is extracting: “1” & “ElecMeter_min” & “3”

this is the code I use

sscanf((*STARTPOINT*),"[{\"modemOrderId\":%s,\"orderType\":\"%s\",\"order\":\"%s\"}]",&modemOrderId[0],&orderType[0],&order[0]);

in the above code, I managed to perfectly define STARTPOINT so I think this part is OK, but I don’t see the data in arrays namely: modemOrderId,orderType and order

why is that?

I think you should post the whole code or at least, definitions of variables involved.

1 Like

here’s an approach tested on my laptop that produces the following output:

  0  1
  1  "ElecMeter_min"
  2  "3"}]

presumably you can clean it up to extract the fields within the quotes

#include <stdio.h>
#include <string.h>

char str [] = "[{\"modemOrderId\":1,\"orderType\":\"ElecMeter_min\",\"order\":\"3\"}]";

#define N_FIELDS 10
char * fields [N_FIELDS];

int
decode (
    char *str,
    char *fields [],
    int   maxFields )
{
    char *t;
    int   n = 0;

    for (t = strtok (str, ","); t != NULL; t = strtok (NULL, ","))  {
        fields [n++] = 2+strstr (t, "\":");
        if (N_FIELDS <= n)
            break;
    }
    return n;
}

int
main ()
{
    int n = decode (str, fields, N_FIELDS);

    for (unsigned i = 0; i < n; i++)
        printf ("  %d  %s\n", i, fields [i]);

    return 0;
}

Here is some example code using SafeString methods to parse the input.
The output is

1 ElecMeter_min 3

Code

// orderParser.ino

// download SafeString library from Arduino library manager
// or from the tutorial page
// https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html
#include "SafeString.h"

char str [] = "[{\"modemOrderId\":1,\"orderType\":\"ElecMeter_min\",\"order\":\"3\"}]";

int Id;
char Type[20];  // try char Type[2] to see how SafeString handles errors
char order[10];

createSafeString(sfStr, 80); // can hold 80 char of input


bool parseInput(SafeString& inputStr) {
  cSF(input, inputStr.length()); //  hold initial input for error msgs
  input = inputStr; // take a copy be case nextToken removes tokens from str

  cSF(token, 20); // createSafeString short cut for 20 char token

  input.nextToken(token, "{}:,"); // break at { } : and ,
  token.debug(F("first token, ignore ["));  // this debug statement suppressed if SafeString::setOutput(Serial);  is commented out

  input.nextToken(token, "{}:,"); // steps over delimiter and gets next token
  token.debug(F("first field"));
  if (token != "\"modemOrderId\"") {
    Serial.print(F("invalid input '")); Serial.print(inputStr); Serial.println("'");
    return false;
  }
  input.nextToken(token, "{}:,"); // steps over delimiter and gets next token
  token.debug(F("modemOrderId"));
  if (!token.toInt(Id)) {
    Serial.print(F("invalid input '")); Serial.print(inputStr); Serial.println("'");
    return false;
  }
  input.nextToken(token, "{}:,"); // steps over delimiter and gets next token
  token.debug(F("second field"));
  if (token != "\"orderType\"") {
    Serial.print(F("invalid input '")); Serial.print(inputStr); Serial.println("'");
    return false;
  }
  input.nextToken(token, "{}:,"); // steps over delimiter and gets next token
  token.debug(F("orderType"));
  cSFA(sfType, Type); // wrap in as SafeString
  token.nextToken(sfType, "\""); // remove "

  input.nextToken(token, "{}:,"); // steps over delimiter and gets next token
  token.debug(F("third field"));
  if (token != "\"order\"") {
    Serial.print(F("invalid input '")); Serial.print(inputStr); Serial.println("'");
    return false;
  }
  input.nextToken(token, "{}:,"); // steps over delimiter and gets next token
  token.debug(F("order"));
  cSFA(sforder, order); // wrap in as SafeString
  token.nextToken(sforder, "\""); // remove "

  // could also parse order
  int orderInt = 0;
  if (!sforder.toInt(orderInt)) {
    Serial.print(F("invalid orderInt '")); Serial.print(inputStr); Serial.println("'");
    return false;
  }
  if (token.hasError()) {
    Serial.println(F("SafeString error detected!!"));
    return false;
  }
  return true; // all OK
}

void setup() {
  Serial.begin(9600);
  for (int i = 10; i > 0; i--) { // pause a little to give you time to open the Arduino monitor
    Serial.print(i); Serial.print(' '); delay(500);
  }
  Serial.println();
  // uncomment next line to get lots of debug msgs and any error msgs
  //SafeString::setOutput(Serial); // enable error messages and SafeString.debug() output to be sent to Serial

  // read input see Arduino Software Solutions https://www.forward.com.au/pfod/ArduinoProgramming/SoftwareSolutions/index.html
  // for alternatives
  sfStr = str;  // for now just assign test data to as SafeString

  if (parseInput(sfStr)) {
    //print results
    Serial.print(Id); Serial.print(" ");     Serial.print(Type); Serial.print(" ");  Serial.print(order); Serial.println();
  } else {
    // format error
    Serial.print(F("invalid input '")); Serial.print(sfStr); Serial.println("'");
  }

}


void loop () {
}

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