New to parsing and need a simple way to parse the following. I tried ArduinoJson but it seems to need the string to have \ in the string to work. {“sensor”:“gps”,“time”:1351824120,“data”:[48.756080,2.302038]}
{“results”:{“sunrise”:“1:32:01 PM”,“sunset”:“11:35:14 PM”,“solar_noon”:“6:33:38 PM”,“day_length”:“10:03:13”,“civil_twilight_begin”:“1:03:37 PM”,“civil_twilight_end”:“12:03:38 AM”,“nautical_twilight_begin”:“12:31:25 PM”,“nautical_twilight_end”:“12:35:51 AM”,“astronomical_twilight_begin”:“11:59:52 AM”,“astronomical_twilight_end”:“1:07:24 AM”},“status”:“OK”}
This is what I have so far.
#define BLYNK_PRINT Serial
// Allow for receiving messages up to 512 bytes long
//#define BLYNK_MAX_READBYTES 512
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ArduinoJson.h>
char auth[] = "*****************************************";
// Your WiFi credentials.
char ssid[] = "*************";
char pass[] = "******************";
StaticJsonBuffer<400> jsonBuffer;
BLYNK_WRITE(V0)
{
Serial.println("WebHook data:");
Serial.println(param.asStr());
char* json =
"({"results":{"sunrise":"1:32:01 PM","sunset":"11:35:14 PM","solar_noon":"6:33:38 PM","day_length":"10:03:13","civil_twilight_begin":"1:03:37 PM","civil_twilight_end":"12:03:38 AM","nautical_twilight_begin":"12:31:25 PM","nautical_twilight_end":"12:35:51 AM","astronomical_twilight_begin":"11:59:52 AM","astronomical_twilight_end":"1:07:24 AM"},"status":"OK"})"; // data received from webhook
JsonObject& root = jsonBuffer.parseObject(json);
// Test if parsing succeeds.
if (!root.success()) {
Serial.println("parseObject() failed");
return;
}
JsonObject& results = root["results"];
const char* sunrise = results["sunrise"]; // "7:24:03 AM"
const char* sunset = results["sunset"]; // "5:36:15 PM"
const char* solar_noon = results["solar_noon"]; // "12:30:09 PM"
const char* day_length = results["day_length"]; // "10:12:12"
const char* civil_twilight_begin = results["civil_twilight_begin"]; // "6:56:34 AM"
const char* civil_twilight_end = results["civil_twilight_end"]; // "6:03:45 PM"
const char* nautical_twilight_begin = results["nautical_twilight_begin"]; // "6:25:18 AM"
const char* nautical_twilight_end = results["nautical_twilight_end"]; // "6:35:01 PM"
const char* astronomical_twilight_begin = results["astronomical_twilight_begin"]; // "5:54:38 AM"
const char* astronomical_twilight_end = results["astronomical_twilight_end"]; // "7:05:40 PM"
const char* status = root["status"]; // "OK"
// Print values.
Serial.print("sunrise\t\t"); Serial.println(sunrise);
Serial.print("sunset\t\t"); Serial.println(sunset);
Serial.print("day length\t"); Serial.println(day_length);
}
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
Blynk.virtualWrite(V0, 1);
}
void loop()
{
Blynk.run();
}
Costas
January 29, 2017, 4:32pm
2
That’s the one I am trying. I can’t get the results from the web hood into the correct format.
I need the data that is in: (param.asStr()) into: char json [ ] = ???
This is how the data comes in from the webhook get:
{“results”:{“sunrise”:“1:32:01 PM”,“sunset”:“11:35:14 PM”,“solar_noon”:“6:33:38 PM”,“day_length”:“10:03:13”,“civil_twilight_begin”:“1:03:37 PM”,“civil_twilight_end”:“12:03:38 AM”,“nautical_twilight_begin”:“12:31:25 PM”,“nautical_twilight_end”:“12:35:51 AM”,“astronomical_twilight_begin”:“11:59:52 AM”,“astronomical_twilight_end”:“1:07:24 AM”},“status”:“OK”}
Costas
January 29, 2017, 5:23pm
5
I do remember parsing the sunrise json from Webhook not long after the widget was introduced.
I did a quick search through my many sketches in the last hour but I couldn’t find the parsing.
All I can say from memory is it worked.
If you struggle in the coming days I can perhaps do a thorough search from my sketches.
@Lichtsignaal and I did some manual parsing and I did find some of this code but I wouldn’t recommend it.
At this point I will take any help or suggestions on how to parse it…
Thank you,
The great thing about your code is that it can be easily adapted for all the many, great API’s that are currently available.
I did think about making the sketch more generic whereby you would enter a keyword in the sketch or say Blynk Terminal such as “Sunrise”, an offset for the index where the data for the keyword is located and a few integer variables to pull out the data i.e. to replace 1, 8 for example in the following line:
if(inputString.substring(1, 8) == "sunrise"){
That is how I fixed it. Well, a while ago anyway. It’s a pain in the butt to get it going good. I’ve changed it a little bit along the way, but basically this is it.
Costas
January 29, 2017, 7:16pm
8
@tbarritt notice Benoit replied on GitHub. He is pretty good and on my “watch list”.
Dynamic buffers is a regular fix and the json Assistant he has at https://bblanchon.github.io/ArduinoJson/assistant/ looks excellent. Ready made parser, fantastic.
I finally got it to work… now I need to adjust the time from UTC -6 hours and get it so Input Time will use it.
WebHook data:
{“results”:{“sunrise”:“1:28:02 PM”,“sunset”:“11:41:01 PM”,“solar_noon”:“6:34:32 PM”,“day_length”:“10:12:59”,“civil_twilight_begin”:“12:59:58 PM”,“civil_twilight_end”:“12:09:05 AM”,“nautical_twilight_begin”:“12:28:01 PM”,“nautical_twilight_end”:“12:41:02 AM”,“astronomical_twilight_begin”:“11:56:40 AM”,“astronomical_twilight_end”:“1:12:23 AM”},“status”:“OK”}
sunrise 1:28:02 PM
sunset 11:41:01 PM
day length 10:12:59
Hi.
See the json assistant over here :https://arduinojson.org/assistant/
Also:
BLYNK_WRITE(V0) {
String jsonString = param.asStr();
Serial.println(“WebHook data:” + jsonString);
JsonObject& root = jsonBuffer.parseObject(jsonString);
// Test if parsing succeeds.
if (!root.success()) {
Serial.println(“parseObject() failed”);
return;
}
JsonObject& results = root[“results”];
const char* results_sunrise = results[“sunrise”]; // “10:29:13 AM”
const char* results_sunset = results[“sunset”]; // “11:25:40 PM”
const char* results_solar_noon = results[“solar_noon”]; // “4:57:27 PM”
const char* results_day_length = results[“day_length”]; // “12:56:27”
const char* results_civil_twilight_begin = results[“civil_twilight_begin”]; // “9:59:07 AM”
const char* results_civil_twilight_end = results[“civil_twilight_end”]; // “11:55:47 PM”
const char* results_nautical_twilight_begin = results[“nautical_twilight_begin”]; // “9:23:02 AM”
const char* results_nautical_twilight_end = results[“nautical_twilight_end”]; // “12:31:51 AM”
const char* results_astronomical_twilight_begin = results[“astronomical_twilight_begin”]; // “8:45:09 AM”
const char* results_astronomical_twilight_end = results[“astronomical_twilight_end”]; // “1:09:44 AM”
const char* status = root[“status”]; // “OK”
// Send it back
terminal.println(“JSON decoded:”);
terminal.print("sunrise: ");
terminal.println(results_sunrise);
terminal.print("sunset: ");
terminal.println(results_sunrise);
terminal.println();
// Ensure everything is sent
terminal.flush();
}
Have a nice day.
Marc.