-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseJson.cpp
More file actions
31 lines (29 loc) · 774 Bytes
/
Copy pathparseJson.cpp
File metadata and controls
31 lines (29 loc) · 774 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//
// Parse a JSON string from getCurrency passed as parameter
// Extract EUR->GBP and EUR->TWD
// Calculate GBP->TWD
//
// Returns: double with conversion value
//
#include "Arduino.h"
#include <ArduinoJson.h>
#include "parseJson.h"
#include "DebugMacros.h"
double parse_json(String r)
{
// Allocate JsonBuffer
// Use arduinojson.org/assistant to compute the capacity.
const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
DynamicJsonBuffer jsonBuffer(capacity);
// Parse JSON object
JsonObject& root = jsonBuffer.parseObject(r);
if (!root.success()) {
DPRINTLN(F("Parsing failed!"));
return 0;
}
else {
double GBPrate = root["rates"]["GBP"];
double TWDrate = root["rates"]["TWD"];
return TWDrate / GBPrate;
}
}