Handling a negative number from JSON

Hi All,

I'm starting to get the hang of this but hit a slight snag on my WEMOS D1 Mini.
I'm a little lost on how to get the desired output format of some data from a GET processed by ArduinoJson.h

client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");

The code parses a data string returned from a url in on eof the following formats:

Positive percentage:


Or Negative percentage:


JSON Code:

  // Get JSON data
  jsonAnswer = answer.substring(jsonIndex);
  //Serial.println("JSON answer: ");
  int newIndex = jsonAnswer.indexOf("percent");
  String chgString = jsonAnswer.substring(newIndex + 11, newIndex + 15);
   Serial.print("chgString = ");

In the first instance (positive), I get the string chgString set correctly to "1.46"
However, when the string is negative and has a "-" in front of it I obviously lose a character to that and get "-0.3" instead missing a decimal point.

My question is how do I handle this issue?

The only way I can think of currently is to allow for an extra character so take characters 11-16 from string then do some kind of check to see the starting character of the string then if it starts with a "-" somehow print 4 characters of the string else print all 5 characters?

Is this the best way or is there something better?
Any chance of some pointers? and to push my luck maybe some basic code thrown in too?


Hi pobrika

You could add another index (commaIndex) and do an IndexOf(",")
so now with newIndex and commaIndex you can work out the length of substring needed for your value