Files
ArduinoJson/examples/JsonParserExample/JsonParserExample.ino

42 lines
919 B
Arduino
Raw Normal View History

// Copyright Benoit Blanchon 2014-2016
2014-11-03 21:29:55 +01:00
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
2014-03-01 12:59:45 +01:00
2014-11-03 21:29:55 +01:00
#include <ArduinoJson.h>
2014-03-01 12:59:45 +01:00
2014-11-03 21:29:55 +01:00
void setup() {
Serial.begin(9600);
while (!Serial) {
// wait serial port initialization
}
2014-07-03 14:00:51 +02:00
2014-11-03 21:29:55 +01:00
StaticJsonBuffer<200> jsonBuffer;
2014-03-01 12:59:45 +01:00
2014-11-03 21:29:55 +01:00
char json[] =
"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
2014-03-01 12:59:45 +01:00
2014-11-03 21:29:55 +01:00
JsonObject& root = jsonBuffer.parseObject(json);
2014-03-01 12:59:45 +01:00
2014-11-03 21:29:55 +01:00
if (!root.success()) {
Serial.println("parseObject() failed");
return;
}
2014-03-01 12:59:45 +01:00
2014-11-03 21:29:55 +01:00
const char* sensor = root["sensor"];
long time = root["time"];
double latitude = root["data"][0];
double longitude = root["data"][1];
2014-03-01 12:59:45 +01:00
2014-11-03 21:29:55 +01:00
Serial.println(sensor);
Serial.println(time);
Serial.println(latitude, 6);
Serial.println(longitude, 6);
2014-03-01 12:59:45 +01:00
}
2014-11-03 21:29:55 +01:00
void loop() {
// not used in this example
2015-08-20 15:15:59 +02:00
}