how can I get the data inside [ ] from api , some like "weather_descriptions":["Partly cloudy"]
Accepted Reply
OK, so that will work.
But iy is not robust if you get from JSON tomorrow 2 strings, such as "Humid" and "hot.
To close the thread, find the correct answer and click "Mark as correct"
Replies
Can you clarify ?
"weather_descriptions":["Partly cloudy"]
Is this part of a dictionary ?
In that case, dictionary is of type [String : [String]]
var myDict : [String : [String]]
First is the key, second part [String] is value.
To get the value, access fwith its key:
if let valueArray = myDict["weather_descriptions"] {
let value = valueArray[0] // "Partly cloudy"
}
The key must be the exact string (incl uppercases and lowercases)
valueArray is ["Partly cloudy"]
Or all at once:
if let value = myDict["weather_descriptions"]?[0] {
print("weather is", value)
}
to get
weather is Partly cloudy
Thanks Claude,
Yes, this is part of array that from API json weatherStack, as I try to make a weatherApp.
I can get data from
"current" : {
"temperature" : 22 ,
"wind_dir" : "SSW" ,
"weather_descriptons" : ["Partly cloudy"] ,
}
for temperature and wind_dir like this
if let current = json["current"] as? [ String : AnyObject] {
if let temp = current["temperature"] as? Int { self.temperture = temp}
if let windD = current["wind_dir"] as? Int { self.windDir = windD}
}
but not for "weather_descriptions"
if let Condition = current["weather_descriptions"] as? String {self.condition = Condition }
🤔
In most cases you’re better off decoding JSON using
Codable
. For example:
let jsonData = Data("""
{
"current" : {
"temperature" : 22 ,
"wind_dir" : "SSW" ,
"weather_descriptons" : ["Partly cloudy"] ,
}
}
""".utf8)
struct Current: Codable {
var temperature: Int
var windDir: String
var weatherDescriptons: [String]
}
struct Response: Codable {
var current: Current
}
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let response = try! decoder.decode(Response.self, from: jsonData)
print(response.current.weatherDescriptons) // -> ["Partly cloudy"]
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
current["weather_descriptions"] is [String]: an array, not a string.
Claude31, I see with thanks🙂
Thanks Eskimo, will use this method next time 🙂
If done, don't forget to close the thread.
Good continuation.
Finally, I can get the data as follow
if let conditions = current["weather_descriptions"] as? Array<String> {
for condition in conditions {
self. condition = condition
}
}
condition is an label string.
Thank you all , again !
Great, good continuation.
Something not clear however:
if there are several condition in current["weather_descriptions"], then you load self.condition several times but just retain the last.
To handle multiple conditions, you could write:
self. condition = "" // reset
if let conditions = current["weather_descriptions"] as? [String] {
for condition in conditions {
if !self.condition.isEmpty { // let's add a separator before appending the new one
self.condition += ", "
}
self.condition += condition // Now, we can append properly the next condition
}
}
Don't forget to close the thread.
Thanks, actually there is only one condition, I use conditions is just for identical
buy the way, how I close this thread? I found no button doing this 😕
OK, so that will work.
But iy is not robust if you get from JSON tomorrow 2 strings, such as "Humid" and "hot.
To close the thread, find the correct answer and click "Mark as correct"
Thanks again 🙂