1

Probably something easy but I’ve not resolved it.

I’m extracting data from a JSON string and all the values are correct accept for two.

They are referring to two date time values and are in milliseconds.

One is the date refreshed and the other is date updated.

Problem is they are both very large and I’m having difficulty assigning them in the IDE.

One is 1588447162117 which is showing as 020-05-02 20:19:53.948 on the dashboard using the JSON.

Here is the code I have thus far to get the data from JSON:

#include <ArduinoJson.h>

void setup() {
  Serial.begin(4800);

  const char* input = "{TotalCases:3308678,TotalCasesToday:4458,TotalDeaths:234123,TotalDeathsToday:293,TotalRecovered:1042991,TotalTerritories:124,CurrentUpdatedMS:1588312648603,CurrentChangedMS:1588312648603}";

  StaticJsonDocument<512> doc;

  DeserializationError err = deserializeJson(doc, input);

  if (err) {
    Serial.print("Error: ");
    Serial.println(err.c_str());
    return;
  }

  unsigned long TotalCases = doc["TotalCases"];
  unsigned long TotalCasesToday = doc["TotalCasesToday"];
  unsigned long TotalDeaths = doc["TotalDeaths"];
  unsigned long TotalDeathsToday = doc["TotalDeathsToday"];
  unsigned long TotalRecovered = doc["TotalRecovered"];
  unsigned long TotalTerritories = doc["TotalTerritories"];
  unsigned long CurrentUpdatedMS = doc["CurrentUpdatedMS"];
  unsigned long CurrentChangedMS = doc["CurrentChangedMS"];

  Serial.print("Total Cases: ");
  Serial.println(TotalCases);
  Serial.print("Total Cases Today: ");
  Serial.println(TotalCasesToday);
  Serial.print("Total Deaths: ");
  Serial.println(TotalDeaths);
  Serial.print("Total Deaths Today: ");
  Serial.println(TotalDeathsToday);
  Serial.print("Total Recovered: ");
  Serial.println(TotalRecovered);
  Serial.print("Total Territories: ");
  Serial.println(TotalTerritories);
  Serial.print("Update time in ms:");
  Serial.println(CurrentUpdatedMS);
  Serial.print("Change time in ms:");
  Serial.print(CurrentChangedMS);
}

void loop()
{

}

**Serial monitor output**

Total Cases: 3308678
Total Cases Today: 4458
Total Deaths: 234123
Total Deaths Today: 293
Total Recovered: 1042991
Total Territories: 124
Update time in ms:0
Change time in ms:0

Should be good to manipulate it once I can work with it in a variable.

Thanks for any input,

Shane

9
  • what exactly is your question? – jsotola May 2 '20 at 20:30
  • I guess you want to use uint64_t but depending on your hardware that might not be possible – Codebreaker007 May 2 '20 at 20:32
  • What JSON library are you using? How did you code the reading of the value? Note that that number is about 2^40.53, so it won't fit in 32 bits. But it will if you drop the last 3 digits, which reduces the resolution to one second. – Edgar Bonet May 2 '20 at 20:33
  • Please show your code – chrisl May 2 '20 at 20:36
  • that number is hexadecimal 171D6D42F05 ... it fits into 64 bits – jsotola May 2 '20 at 20:37
2

I didn't try myself, but just by looking at the docs I suggest you try:

  1. Adding
#define ARDUINOJSON_USE_LONG_LONG 1

at the top of the sketch, before including ArduinoJson.h.

  1. Declare CurrentUpdatedMS and CurrentUpdatedMS as either unsigned long long or uint64_t.

Edit: To answer the question in the comment:

When you said you looked at the docs where did you check?

I had to follow a (not so simple) path: Deserialization tutorialJsonDocument::operator[]JsonVariantJsonVariant::as<T>(). This last page states that the method as<unsigned long long>() “may require ARDUINOJSON_USE_LONG_LONG”. Unfortunately, the macro is not linked, so I have to search for it and found ARDUINOJSON_USE_LONG_LONG.

2
  • Thanks I have it working now and will process it like that. When you said you looked at the docs where did you check? Just so I can know where to look in future. – Lifesigns May 3 '20 at 6:55
  • Hi. That’s worked well as I could assign the value though it is a UNIX timestamp and not appearing correctly when I print or display on a OLED. Tried a couple of things to remedy this including casting to long, u_int 64 etc but it still returns the incorrect time. My data has dateupdated in MS as 13 digits when I should be able to process 10. Would appreciate any thoughts I’ve tried various options. Is there a way to convert that object in the JSON to a string? – Lifesigns May 8 '20 at 16:32

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.