Is there a trick to pulling a string out of a JSON data line vs. a numerical variable? I am pulling information from openweathermap.org. I won't give you all the code because it is working ok but I cannot get the "[weather][main]" line to work and I can't seem to figure out the error:
double temp = (double) api_object["main"]["temp"];
String cond = api_object["weather"]["main"];
the temp line works just fine, matter of fact any line with numbers is working but I can't get the strings to come over and I have tried almost everything I can find in the JSON.h library. Is it my use of "String"? If someone can provide a nudge that would be great!!
String from a JSON data line
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- dastels
- Posts: 15655
- Joined: Tue Oct 20, 2015 3:22 pm
Re: String from a JSON data line
What JSON library are you using?
Dave
Dave
- EBCrp
- Posts: 16
- Joined: Fri Nov 11, 2022 6:19 pm
Re: String from a JSON data line
#include <Arduino_JSON.h>
- dastels
- Posts: 15655
- Joined: Tue Oct 20, 2015 3:22 pm
Re: String from a JSON data line
What if you used const char* rather than String?
Dave
Dave
- EBCrp
- Posts: 16
- Joined: Fri Nov 11, 2022 6:19 pm
Re: String from a JSON data line
I get a blank response .
- dastels
- Posts: 15655
- Joined: Tue Oct 20, 2015 3:22 pm
Re: String from a JSON data line
What is the jason that you are extracting the data from? Specifically the ["weather"]["main"] path.
Dave
Dave
- EBCrp
- Posts: 16
- Joined: Fri Nov 11, 2022 6:19 pm
Re: String from a JSON data line
I have attached part of the sketch and also part of the serial monitor response (at the bottom) with what I think you are asking for. Thanks for your help!!
- Attachments
-
- JSON File.txt
- Partial sketch and results
- (1.91 KiB) Downloaded 2 times
- EBCrp
- Posts: 16
- Joined: Fri Nov 11, 2022 6:19 pm
Re: String from a JSON data line
Resolved!! In case someone else has the question this is what finally resolved the issue:
String conditions = api_object["weather"][0]["main"];
Note the [0] between "weather" and "main". Now I just need to find out what it means!! :-)
String conditions = api_object["weather"][0]["main"];
Note the [0] between "weather" and "main". Now I just need to find out what it means!! :-)
- dastels
- Posts: 15655
- Joined: Tue Oct 20, 2015 3:22 pm
Re: String from a JSON data line
It means that api_object["weather"] results in a json array [0] pulls out the first element which is json dictionary. It is clearer if the json is prettyprinted:
Dave
Code: Select all
{
"coord": {
"lon": -82.9988,
"lat": 39.9612
},
"weather": [
{
"id": 803,
"main": "Clouds",
"description": "broken clouds",
"icon": "04d"
}
],
"base": "stations",
"main": {
"temp": 29.08,
"feels_like": 22.05,
"temp_min": 26.62,
"temp_max": 31.8,
"pressure": 1024,
"humidity": 85
},
"visibility": 10000,
"wind": {
"speed": 6.91,
"deg": 110
},
"clouds": {
"all": 75
},
"dt": 1673185633,
"sys": {
"type": 2,
"id": 2079804,
"country": "US",
"sunrise": 1673182418,
"sunset": 1673216591
},
"timezone": -18000,
"id": 4509177,
"name": "Columbus",
"cod": 200
}
Please be positive and constructive with your questions and comments.