|
|
@@ -1,40 +1,37 @@
|
|
|
-/*
|
|
|
-* Arduino JSON library - Parser Example
|
|
|
-* Benoit Blanchon 2014 - MIT License
|
|
|
-*/
|
|
|
+// Copyright Benoit Blanchon 2014
|
|
|
+// MIT License
|
|
|
+//
|
|
|
+// Arduino JSON library
|
|
|
+// https://github.com/bblanchon/ArduinoJson
|
|
|
|
|
|
-#include <JsonParser.h>
|
|
|
+#include <ArduinoJson.h>
|
|
|
|
|
|
-using namespace ArduinoJson::Parser;
|
|
|
+void setup() {
|
|
|
+ Serial.begin(9600);
|
|
|
|
|
|
-void setup()
|
|
|
-{
|
|
|
- Serial.begin(9600);
|
|
|
+ StaticJsonBuffer<200> jsonBuffer;
|
|
|
|
|
|
- char json [] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
|
|
|
+ char json[] =
|
|
|
+ "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
|
|
|
|
|
|
- JsonParser<16> parser;
|
|
|
+ JsonObject& root = jsonBuffer.parseObject(json);
|
|
|
|
|
|
- JsonObject root = parser.parse(json);
|
|
|
+ if (!root.success()) {
|
|
|
+ Serial.println("parseObject() failed");
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- if (!root.success())
|
|
|
- {
|
|
|
- Serial.println("JsonParser.parse() failed");
|
|
|
- return;
|
|
|
- }
|
|
|
+ const char* sensor = root["sensor"];
|
|
|
+ long time = root["time"];
|
|
|
+ double latitude = root["data"][0];
|
|
|
+ double longitude = root["data"][1];
|
|
|
|
|
|
- char* sensor = root["sensor"];
|
|
|
- long time = root["time"];
|
|
|
- double latitude = root["data"][0];
|
|
|
- double longitude = root["data"][1];
|
|
|
-
|
|
|
- Serial.println(sensor);
|
|
|
- Serial.println(time);
|
|
|
- Serial.println(latitude, 6);
|
|
|
- Serial.println(longitude, 6);
|
|
|
+ Serial.println(sensor);
|
|
|
+ Serial.println(time);
|
|
|
+ Serial.println(latitude, 6);
|
|
|
+ Serial.println(longitude, 6);
|
|
|
}
|
|
|
|
|
|
-void loop()
|
|
|
-{
|
|
|
-
|
|
|
+void loop() {
|
|
|
+ // not used in this example
|
|
|
}
|