I am using the demo from the library, and modified it with my data. I am able to extract most of the data, except for the text string for weather.
Here is the code:
#include <ArduinoJson.h>
void setup() {
Serial.begin(115200);
while (!Serial) continue;
Serial.println();
// Allocate the JSON document
//
// Inside the brackets, 600 is the capacity of the memory pool in bytes.
// Don't forget to change this value to match your JSON document.
// Use arduinojson.org/v6/assistant to compute the capacity.
StaticJsonDocument<600> doc;
// JSON input string.
//
// Using a char[], as shown here, enables the "zero-copy" mode. This mode uses
// the minimal amount of memory because the JsonDocument stores pointers to
// the input buffer.
// If you use another type of input, ArduinoJson must copy the strings from
// the input to the JsonDocument, so you need to increase the capacity of the
// JsonDocument.
char json[] = "{\"coord\":{\"lon\":-71.45,\"lat\":42.56},\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"base\":\"stations\",\"main\":{\"temp\":52.41,\"pressure\":1024,\"humidity\":81,\"temp_min\":50,\"temp_max\":55},\"visibility\":14484,\"wind\":{\"speed\":5.82,\"deg\":360},\"rain\":{},\"clouds\":{\"all\":90},\"dt\":1572291729,\"sys\":{\"type\":1,\"id\":3530,\"country\":\"US\",\"sunrise\":1572261244,\"sunset\":1572299095},\"timezone\":-14400,\"id\":4955219,\"name\":\"Westford\",\"cod\":200}";
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, json);
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
// Fetch values.
double temperature = doc["main"]["temp"];
long pressure = doc["main"]["pressure"];
int humidity = doc["main"]["humidity"];
int tempMin = doc["main"]["temp_min"];
int tempMax = doc["main"]["temp_max"];
double latitude = doc["coord"]["lat"];
double longitude = doc["coord"]["lon"];
const char* weather = doc["weather"]["description"];
// Print values.
Serial.println();
Serial.print(F("Temperature: "));
Serial.println(temperature);
Serial.print(F("Pressure: "));
Serial.println(pressure);
Serial.print(F("Temp min: "));
Serial.println(tempMin);
Serial.print(F("Temp max: "));
Serial.println(tempMax);
Serial.print(F("Latitude: "));
Serial.println(latitude);
Serial.print(F("Longitude: "));
Serial.println(longitude);
Serial.print(F("Weather: "));
Serial.println(weather);
}
void loop() {
// not used in this example
}
What am I doing wrong with the code extracting weather?
Here is the json data formatted:
{
"coord":{
"lon":-71.45,
"lat":42.56
},
"weather":[
{
"id":500,
"main":"Rain",
"description":"light rain",
"icon":"10d"
}
],
"base":"stations",
"main":{
"temp":52.41,
"pressure":1024,
"humidity":81,
"temp_min":50,
"temp_max":55
},
"visibility":14484,
"wind":{
"speed":5.82,
"deg":360
},
"rain":{
},
"clouds":{
"all":90
},
"dt":1572291729,
"sys":{
"type":1,
"id":3530,
"country":"US",
"sunrise":1572261244,
"sunset":1572299095
},
"timezone":-14400,
"id":4955219,
"name":"Westford",
"cod":200
}