JSON request is not returning data
I'm trying to create a weather application. I followed a tutorial about JSON and Swift 4 Decodable, I followed the tutorial and it worked. The problem is that when I'm trying to put my own URL to make a request, it won't work.
This is my code:
class MainVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
let jsonUrlString: String = "https://api.darksky.net/forecast/APIKEY/37.8267,-122.4233"
private func getForecast(){
guard let url = URL(string: jsonUrlString) else { return }
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else { return }
do{
let weatherForecast = try JSONDecoder().decode(Weather.self, from: data)
print(weatherForecast)
}catch _ as NSError{
}
}.resume()
}
}
Error Log:
Error Domain=NSCocoaErrorDomain Code=4864 "Expected to decode Array<Any> but found a dictionary instead." UserInfo={NSCodingPath=(
), NSDebugDescription=Expected to decode Array<Any> but found a dictionary instead.}
How can I parse this https://darksky.net/dev/docs#forecast-request ? I only need the few things from the Daily sections but It seems like an array inside an array to me.
json swift
|
show 6 more comments
I'm trying to create a weather application. I followed a tutorial about JSON and Swift 4 Decodable, I followed the tutorial and it worked. The problem is that when I'm trying to put my own URL to make a request, it won't work.
This is my code:
class MainVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
let jsonUrlString: String = "https://api.darksky.net/forecast/APIKEY/37.8267,-122.4233"
private func getForecast(){
guard let url = URL(string: jsonUrlString) else { return }
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else { return }
do{
let weatherForecast = try JSONDecoder().decode(Weather.self, from: data)
print(weatherForecast)
}catch _ as NSError{
}
}.resume()
}
}
Error Log:
Error Domain=NSCocoaErrorDomain Code=4864 "Expected to decode Array<Any> but found a dictionary instead." UserInfo={NSCodingPath=(
), NSDebugDescription=Expected to decode Array<Any> but found a dictionary instead.}
How can I parse this https://darksky.net/dev/docs#forecast-request ? I only need the few things from the Daily sections but It seems like an array inside an array to me.
json swift
Could you log the errors and share those? (err
in the data task response block and the error you are now ignoring in the catch block)
– silicon_valley
Nov 25 '18 at 16:45
I don't get any errors :
– John Doah
Nov 25 '18 at 16:47
If you don't get any errors, then what is printed when you printweatherForecast
?
– silicon_valley
Nov 25 '18 at 16:52
Dont you need to update 'APIKEY' in your url with an actual apikey? Like: private let apiKey = "your api key here". Then: let jsonUrlString: String = "api.darksky.net/forecast/(apiKey)/37.8267,-122.4233"
– emrepun
Nov 25 '18 at 16:52
@silicon_valley nothing is printed, I guess there is something with the Json, should I only need few things from the Json request (day, and temp) for now, maybe this is the problem?
– John Doah
Nov 25 '18 at 16:58
|
show 6 more comments
I'm trying to create a weather application. I followed a tutorial about JSON and Swift 4 Decodable, I followed the tutorial and it worked. The problem is that when I'm trying to put my own URL to make a request, it won't work.
This is my code:
class MainVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
let jsonUrlString: String = "https://api.darksky.net/forecast/APIKEY/37.8267,-122.4233"
private func getForecast(){
guard let url = URL(string: jsonUrlString) else { return }
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else { return }
do{
let weatherForecast = try JSONDecoder().decode(Weather.self, from: data)
print(weatherForecast)
}catch _ as NSError{
}
}.resume()
}
}
Error Log:
Error Domain=NSCocoaErrorDomain Code=4864 "Expected to decode Array<Any> but found a dictionary instead." UserInfo={NSCodingPath=(
), NSDebugDescription=Expected to decode Array<Any> but found a dictionary instead.}
How can I parse this https://darksky.net/dev/docs#forecast-request ? I only need the few things from the Daily sections but It seems like an array inside an array to me.
json swift
I'm trying to create a weather application. I followed a tutorial about JSON and Swift 4 Decodable, I followed the tutorial and it worked. The problem is that when I'm trying to put my own URL to make a request, it won't work.
This is my code:
class MainVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
let jsonUrlString: String = "https://api.darksky.net/forecast/APIKEY/37.8267,-122.4233"
private func getForecast(){
guard let url = URL(string: jsonUrlString) else { return }
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else { return }
do{
let weatherForecast = try JSONDecoder().decode(Weather.self, from: data)
print(weatherForecast)
}catch _ as NSError{
}
}.resume()
}
}
Error Log:
Error Domain=NSCocoaErrorDomain Code=4864 "Expected to decode Array<Any> but found a dictionary instead." UserInfo={NSCodingPath=(
), NSDebugDescription=Expected to decode Array<Any> but found a dictionary instead.}
How can I parse this https://darksky.net/dev/docs#forecast-request ? I only need the few things from the Daily sections but It seems like an array inside an array to me.
json swift
json swift
edited Nov 25 '18 at 17:04
John Doah
asked Nov 25 '18 at 16:41
John DoahJohn Doah
168519
168519
Could you log the errors and share those? (err
in the data task response block and the error you are now ignoring in the catch block)
– silicon_valley
Nov 25 '18 at 16:45
I don't get any errors :
– John Doah
Nov 25 '18 at 16:47
If you don't get any errors, then what is printed when you printweatherForecast
?
– silicon_valley
Nov 25 '18 at 16:52
Dont you need to update 'APIKEY' in your url with an actual apikey? Like: private let apiKey = "your api key here". Then: let jsonUrlString: String = "api.darksky.net/forecast/(apiKey)/37.8267,-122.4233"
– emrepun
Nov 25 '18 at 16:52
@silicon_valley nothing is printed, I guess there is something with the Json, should I only need few things from the Json request (day, and temp) for now, maybe this is the problem?
– John Doah
Nov 25 '18 at 16:58
|
show 6 more comments
Could you log the errors and share those? (err
in the data task response block and the error you are now ignoring in the catch block)
– silicon_valley
Nov 25 '18 at 16:45
I don't get any errors :
– John Doah
Nov 25 '18 at 16:47
If you don't get any errors, then what is printed when you printweatherForecast
?
– silicon_valley
Nov 25 '18 at 16:52
Dont you need to update 'APIKEY' in your url with an actual apikey? Like: private let apiKey = "your api key here". Then: let jsonUrlString: String = "api.darksky.net/forecast/(apiKey)/37.8267,-122.4233"
– emrepun
Nov 25 '18 at 16:52
@silicon_valley nothing is printed, I guess there is something with the Json, should I only need few things from the Json request (day, and temp) for now, maybe this is the problem?
– John Doah
Nov 25 '18 at 16:58
Could you log the errors and share those? (
err
in the data task response block and the error you are now ignoring in the catch block)– silicon_valley
Nov 25 '18 at 16:45
Could you log the errors and share those? (
err
in the data task response block and the error you are now ignoring in the catch block)– silicon_valley
Nov 25 '18 at 16:45
I don't get any errors :
– John Doah
Nov 25 '18 at 16:47
I don't get any errors :
– John Doah
Nov 25 '18 at 16:47
If you don't get any errors, then what is printed when you print
weatherForecast
?– silicon_valley
Nov 25 '18 at 16:52
If you don't get any errors, then what is printed when you print
weatherForecast
?– silicon_valley
Nov 25 '18 at 16:52
Dont you need to update 'APIKEY' in your url with an actual apikey? Like: private let apiKey = "your api key here". Then: let jsonUrlString: String = "api.darksky.net/forecast/(apiKey)/37.8267,-122.4233"
– emrepun
Nov 25 '18 at 16:52
Dont you need to update 'APIKEY' in your url with an actual apikey? Like: private let apiKey = "your api key here". Then: let jsonUrlString: String = "api.darksky.net/forecast/(apiKey)/37.8267,-122.4233"
– emrepun
Nov 25 '18 at 16:52
@silicon_valley nothing is printed, I guess there is something with the Json, should I only need few things from the Json request (day, and temp) for now, maybe this is the problem?
– John Doah
Nov 25 '18 at 16:58
@silicon_valley nothing is printed, I guess there is something with the Json, should I only need few things from the Json request (day, and temp) for now, maybe this is the problem?
– John Doah
Nov 25 '18 at 16:58
|
show 6 more comments
1 Answer
1
active
oldest
votes
It looks like your Weather struct is built up incorrectly. It should looks something like this:
struct DataItem: Codable {
var summary: String
var uvIndex: Int
}
struct Info: Codable {
var summary: String
var icon: String
var data: [DataItem]
}
struct Weather: Codable {
var daily: Info
var hourly: Info
var timezone: String
}
I've left quite a few items out, but this should help you to get started.
Thanks, I had something exactly like that, but it still didn't work. Well I will test this again tomorrow and try to fix it. Will update of course. Thanks again!
– John Doah
Nov 25 '18 at 20:15
Could you please add your code, otherwise we can't help... The problem is in the JSON deserialisation, so that's the code we need to see to know what is wrong with it.
– silicon_valley
Nov 26 '18 at 20:27
Sorry took me so long to answer, this was the problem, I did had just one struct so I guess it only took the first item from the JSON. Anyway, Thanks again!
– John Doah
Dec 1 '18 at 16:33
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469641%2fjson-request-is-not-returning-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It looks like your Weather struct is built up incorrectly. It should looks something like this:
struct DataItem: Codable {
var summary: String
var uvIndex: Int
}
struct Info: Codable {
var summary: String
var icon: String
var data: [DataItem]
}
struct Weather: Codable {
var daily: Info
var hourly: Info
var timezone: String
}
I've left quite a few items out, but this should help you to get started.
Thanks, I had something exactly like that, but it still didn't work. Well I will test this again tomorrow and try to fix it. Will update of course. Thanks again!
– John Doah
Nov 25 '18 at 20:15
Could you please add your code, otherwise we can't help... The problem is in the JSON deserialisation, so that's the code we need to see to know what is wrong with it.
– silicon_valley
Nov 26 '18 at 20:27
Sorry took me so long to answer, this was the problem, I did had just one struct so I guess it only took the first item from the JSON. Anyway, Thanks again!
– John Doah
Dec 1 '18 at 16:33
add a comment |
It looks like your Weather struct is built up incorrectly. It should looks something like this:
struct DataItem: Codable {
var summary: String
var uvIndex: Int
}
struct Info: Codable {
var summary: String
var icon: String
var data: [DataItem]
}
struct Weather: Codable {
var daily: Info
var hourly: Info
var timezone: String
}
I've left quite a few items out, but this should help you to get started.
Thanks, I had something exactly like that, but it still didn't work. Well I will test this again tomorrow and try to fix it. Will update of course. Thanks again!
– John Doah
Nov 25 '18 at 20:15
Could you please add your code, otherwise we can't help... The problem is in the JSON deserialisation, so that's the code we need to see to know what is wrong with it.
– silicon_valley
Nov 26 '18 at 20:27
Sorry took me so long to answer, this was the problem, I did had just one struct so I guess it only took the first item from the JSON. Anyway, Thanks again!
– John Doah
Dec 1 '18 at 16:33
add a comment |
It looks like your Weather struct is built up incorrectly. It should looks something like this:
struct DataItem: Codable {
var summary: String
var uvIndex: Int
}
struct Info: Codable {
var summary: String
var icon: String
var data: [DataItem]
}
struct Weather: Codable {
var daily: Info
var hourly: Info
var timezone: String
}
I've left quite a few items out, but this should help you to get started.
It looks like your Weather struct is built up incorrectly. It should looks something like this:
struct DataItem: Codable {
var summary: String
var uvIndex: Int
}
struct Info: Codable {
var summary: String
var icon: String
var data: [DataItem]
}
struct Weather: Codable {
var daily: Info
var hourly: Info
var timezone: String
}
I've left quite a few items out, but this should help you to get started.
answered Nov 25 '18 at 17:31
silicon_valleysilicon_valley
1,379915
1,379915
Thanks, I had something exactly like that, but it still didn't work. Well I will test this again tomorrow and try to fix it. Will update of course. Thanks again!
– John Doah
Nov 25 '18 at 20:15
Could you please add your code, otherwise we can't help... The problem is in the JSON deserialisation, so that's the code we need to see to know what is wrong with it.
– silicon_valley
Nov 26 '18 at 20:27
Sorry took me so long to answer, this was the problem, I did had just one struct so I guess it only took the first item from the JSON. Anyway, Thanks again!
– John Doah
Dec 1 '18 at 16:33
add a comment |
Thanks, I had something exactly like that, but it still didn't work. Well I will test this again tomorrow and try to fix it. Will update of course. Thanks again!
– John Doah
Nov 25 '18 at 20:15
Could you please add your code, otherwise we can't help... The problem is in the JSON deserialisation, so that's the code we need to see to know what is wrong with it.
– silicon_valley
Nov 26 '18 at 20:27
Sorry took me so long to answer, this was the problem, I did had just one struct so I guess it only took the first item from the JSON. Anyway, Thanks again!
– John Doah
Dec 1 '18 at 16:33
Thanks, I had something exactly like that, but it still didn't work. Well I will test this again tomorrow and try to fix it. Will update of course. Thanks again!
– John Doah
Nov 25 '18 at 20:15
Thanks, I had something exactly like that, but it still didn't work. Well I will test this again tomorrow and try to fix it. Will update of course. Thanks again!
– John Doah
Nov 25 '18 at 20:15
Could you please add your code, otherwise we can't help... The problem is in the JSON deserialisation, so that's the code we need to see to know what is wrong with it.
– silicon_valley
Nov 26 '18 at 20:27
Could you please add your code, otherwise we can't help... The problem is in the JSON deserialisation, so that's the code we need to see to know what is wrong with it.
– silicon_valley
Nov 26 '18 at 20:27
Sorry took me so long to answer, this was the problem, I did had just one struct so I guess it only took the first item from the JSON. Anyway, Thanks again!
– John Doah
Dec 1 '18 at 16:33
Sorry took me so long to answer, this was the problem, I did had just one struct so I guess it only took the first item from the JSON. Anyway, Thanks again!
– John Doah
Dec 1 '18 at 16:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469641%2fjson-request-is-not-returning-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Could you log the errors and share those? (
err
in the data task response block and the error you are now ignoring in the catch block)– silicon_valley
Nov 25 '18 at 16:45
I don't get any errors :
– John Doah
Nov 25 '18 at 16:47
If you don't get any errors, then what is printed when you print
weatherForecast
?– silicon_valley
Nov 25 '18 at 16:52
Dont you need to update 'APIKEY' in your url with an actual apikey? Like: private let apiKey = "your api key here". Then: let jsonUrlString: String = "api.darksky.net/forecast/(apiKey)/37.8267,-122.4233"
– emrepun
Nov 25 '18 at 16:52
@silicon_valley nothing is printed, I guess there is something with the Json, should I only need few things from the Json request (day, and temp) for now, maybe this is the problem?
– John Doah
Nov 25 '18 at 16:58