Parsing a Char GET response

Hi, could use some help, As a response to a Http GET I receive the following;

startedRequest ok
Got status code: 200
Content length is: 316

Body returned follows:
"postId": 11,
"id": 52,
"name": "esse autem dolorum",
"email": "Abigail.OConnell@june.org",
"body": "et enim voluptatem totam laudantium\nimpedit nam labore repellendus enim earum aut\nconsectetur mollitia fugit qui repellat expedita sunt\naut fugiat vel illo quos aspernatur ducimus"

Using this code...

char c;
       while ( (http.connected() || http.available()) &&
               ((millis() - timeoutStart) < kNetworkTimeout) )
        {
            if (http.available())
            {
                c = http.read();
                // Print out this character
                Serial.print(c);

I would like to extract the name from this text as a char but have no idea where to begin. Thx

By "name" you mean the HTML title?

I don't think so, looks like the third line of the body is required.

I'd read it char by char into an array and whenever you see a comma search that buffer for "name":. If it's found, the remainder of the buffer is the content you're looking for. If not, reset the buffer index before reading more.

Don't forget to check to avoid buffer overflow.

no, the variable "name" in the text

ok,I expected to be able to search for "name" and extract what comes next

First you need something holding the text so that it can be searched, hence the suggestion to use an array of chars

Then maybe something like this if you have no objections to using the String class.

String response = http.responseBody();  
size_t startIndex = response.indexOf("\"name\": \"") + 9;
size_t endIndex = response.indexOf("\"", startIndex);
Serial.println(response.substring(startIndex, endIndex));
void setup() {
  Serial.begin(115200);
}
struct Input {
  uint16_t postId;
  uint16_t id;
  String name;
  String email;
  String body;
} inputString;

void loop() {
  while (!Serial.available());
  if ( Serial.available() > 10)
  {
    inputString.postId = Serial.parseInt();
    inputString.id = Serial.parseInt();
    Serial.readStringUntil(' ');
    Serial.readStringUntil(' ');
    inputString.name = Serial.readStringUntil(',');
    Serial.readStringUntil(' ');
    Serial.readStringUntil(' ');
    inputString.email = Serial.readStringUntil(',');
    Serial.readStringUntil(' ');
    Serial.readStringUntil(' ');
    inputString.body = Serial.readStringUntil(',');
    Serial.flush();
    Serial.println(inputString.postId);
    Serial.println(inputString.id);
    Serial.println(inputString.name);
    Serial.println(inputString.email);
    Serial.println(inputString.body);
  }
}

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