MCPcopy Create free account
hub / github.com/bblanchon/ArduinoJson / main

Function main

extras/scripts/wandbox/JsonParserExample.cpp:10–43  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8#include "ArduinoJson.h"
9
10int main() {
11 // Allocate the JSON document
12 JsonDocument doc;
13
14 // JSON input string
15 const char* json =
16 "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
17
18 // Deserialize the JSON document
19 DeserializationError error = deserializeJson(doc, json);
20
21 // Test if parsing succeeds
22 if (error) {
23 std::cerr << "deserializeJson() failed: " << error.c_str() << std::endl;
24 return 1;
25 }
26
27 // Fetch the values
28 //
29 // Most of the time, you can rely on the implicit casts.
30 // In other case, you can do doc["time"].as<long>();
31 const char* sensor = doc["sensor"];
32 long time = doc["time"];
33 double latitude = doc["data"][0];
34 double longitude = doc["data"][1];
35
36 // Print the values
37 std::cout << sensor << std::endl;
38 std::cout << time << std::endl;
39 std::cout << latitude << std::endl;
40 std::cout << longitude << std::endl;
41
42 return 0;
43}

Callers

nothing calls this directly

Calls 2

deserializeJsonFunction · 0.85
c_strMethod · 0.45

Tested by

no test coverage detected