JSON request is not returning data












0















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.










share|improve this question

























  • 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
















0















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.










share|improve this question

























  • 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














0












0








0








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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



















  • 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

















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












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer
























  • 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











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
});


}
});














draft saved

draft discarded


















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









1














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.






share|improve this answer
























  • 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
















1














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.






share|improve this answer
























  • 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














1












1








1







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)