Swift 3 - Can't retrieve the info after downloading this data
up vote
1
down vote
favorite
I'm trying to access the data downloaded from a website which looks like this:
{"quizzes":[
{"id":1, "question": "Can't solve this :("},
{"id":2, "question": "Someone help pls"}]}
The data downloaded looks way more complex, with more values, more keys, and some keys being associated to another dictionary of Key:String, but since i can't even access the most simple fields I thought i would start with this.
I'm using JSONSerialization, so my question is:
If I want to create a variable where i can save the downloaded data... Which would be it's type? I would say [String:[String:Any]], but I'm not sure if "quizzes" represents a key on this specific key, since the data starts with '{' and not with '['.
ios json swift
New contributor
add a comment |
up vote
1
down vote
favorite
I'm trying to access the data downloaded from a website which looks like this:
{"quizzes":[
{"id":1, "question": "Can't solve this :("},
{"id":2, "question": "Someone help pls"}]}
The data downloaded looks way more complex, with more values, more keys, and some keys being associated to another dictionary of Key:String, but since i can't even access the most simple fields I thought i would start with this.
I'm using JSONSerialization, so my question is:
If I want to create a variable where i can save the downloaded data... Which would be it's type? I would say [String:[String:Any]], but I'm not sure if "quizzes" represents a key on this specific key, since the data starts with '{' and not with '['.
ios json swift
New contributor
i have no resource to write swift 2 , the answer is in NSCoding
– Sh_Khan
Nov 17 at 9:58
Have you actually tried this? Personally I would go for the more generic format [String:Any] and have runtime check the structure of the data as it is parsed
– Spads
Nov 17 at 10:11
Your data type should be[String:[[String:Any]]]
– Sviatoslav Yakymiv
Nov 17 at 10:56
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm trying to access the data downloaded from a website which looks like this:
{"quizzes":[
{"id":1, "question": "Can't solve this :("},
{"id":2, "question": "Someone help pls"}]}
The data downloaded looks way more complex, with more values, more keys, and some keys being associated to another dictionary of Key:String, but since i can't even access the most simple fields I thought i would start with this.
I'm using JSONSerialization, so my question is:
If I want to create a variable where i can save the downloaded data... Which would be it's type? I would say [String:[String:Any]], but I'm not sure if "quizzes" represents a key on this specific key, since the data starts with '{' and not with '['.
ios json swift
New contributor
I'm trying to access the data downloaded from a website which looks like this:
{"quizzes":[
{"id":1, "question": "Can't solve this :("},
{"id":2, "question": "Someone help pls"}]}
The data downloaded looks way more complex, with more values, more keys, and some keys being associated to another dictionary of Key:String, but since i can't even access the most simple fields I thought i would start with this.
I'm using JSONSerialization, so my question is:
If I want to create a variable where i can save the downloaded data... Which would be it's type? I would say [String:[String:Any]], but I'm not sure if "quizzes" represents a key on this specific key, since the data starts with '{' and not with '['.
ios json swift
ios json swift
New contributor
New contributor
New contributor
asked Nov 17 at 9:00
V. Garcia
61
61
New contributor
New contributor
i have no resource to write swift 2 , the answer is in NSCoding
– Sh_Khan
Nov 17 at 9:58
Have you actually tried this? Personally I would go for the more generic format [String:Any] and have runtime check the structure of the data as it is parsed
– Spads
Nov 17 at 10:11
Your data type should be[String:[[String:Any]]]
– Sviatoslav Yakymiv
Nov 17 at 10:56
add a comment |
i have no resource to write swift 2 , the answer is in NSCoding
– Sh_Khan
Nov 17 at 9:58
Have you actually tried this? Personally I would go for the more generic format [String:Any] and have runtime check the structure of the data as it is parsed
– Spads
Nov 17 at 10:11
Your data type should be[String:[[String:Any]]]
– Sviatoslav Yakymiv
Nov 17 at 10:56
i have no resource to write swift 2 , the answer is in NSCoding
– Sh_Khan
Nov 17 at 9:58
i have no resource to write swift 2 , the answer is in NSCoding
– Sh_Khan
Nov 17 at 9:58
Have you actually tried this? Personally I would go for the more generic format [String:Any] and have runtime check the structure of the data as it is parsed
– Spads
Nov 17 at 10:11
Have you actually tried this? Personally I would go for the more generic format [String:Any] and have runtime check the structure of the data as it is parsed
– Spads
Nov 17 at 10:11
Your data type should be
[String:[[String:Any]]]
– Sviatoslav Yakymiv
Nov 17 at 10:56
Your data type should be
[String:[[String:Any]]]
– Sviatoslav Yakymiv
Nov 17 at 10:56
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Instead of using JSONSerialization one could use JSONDecoder.
Example
struct Question: Decodable {
let id: Int
let question: String
}
struct Quiz: Decodable {
let quizzes: [Question]
}
Assuming jsonStr is a string with the JSON in your question:
if let jsonData = jsonStr.data(using: .utf8) {
if let quiz = try? JSONDecoder().decode(Quiz.self, from: jsonData) {
for question in quiz.quizzes {
print("(question.id) "(question.question)"")
}
}
}
Output
1 "Can't solve this :("
2 "Someone help pls"
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Instead of using JSONSerialization one could use JSONDecoder.
Example
struct Question: Decodable {
let id: Int
let question: String
}
struct Quiz: Decodable {
let quizzes: [Question]
}
Assuming jsonStr is a string with the JSON in your question:
if let jsonData = jsonStr.data(using: .utf8) {
if let quiz = try? JSONDecoder().decode(Quiz.self, from: jsonData) {
for question in quiz.quizzes {
print("(question.id) "(question.question)"")
}
}
}
Output
1 "Can't solve this :("
2 "Someone help pls"
add a comment |
up vote
0
down vote
Instead of using JSONSerialization one could use JSONDecoder.
Example
struct Question: Decodable {
let id: Int
let question: String
}
struct Quiz: Decodable {
let quizzes: [Question]
}
Assuming jsonStr is a string with the JSON in your question:
if let jsonData = jsonStr.data(using: .utf8) {
if let quiz = try? JSONDecoder().decode(Quiz.self, from: jsonData) {
for question in quiz.quizzes {
print("(question.id) "(question.question)"")
}
}
}
Output
1 "Can't solve this :("
2 "Someone help pls"
add a comment |
up vote
0
down vote
up vote
0
down vote
Instead of using JSONSerialization one could use JSONDecoder.
Example
struct Question: Decodable {
let id: Int
let question: String
}
struct Quiz: Decodable {
let quizzes: [Question]
}
Assuming jsonStr is a string with the JSON in your question:
if let jsonData = jsonStr.data(using: .utf8) {
if let quiz = try? JSONDecoder().decode(Quiz.self, from: jsonData) {
for question in quiz.quizzes {
print("(question.id) "(question.question)"")
}
}
}
Output
1 "Can't solve this :("
2 "Someone help pls"
Instead of using JSONSerialization one could use JSONDecoder.
Example
struct Question: Decodable {
let id: Int
let question: String
}
struct Quiz: Decodable {
let quizzes: [Question]
}
Assuming jsonStr is a string with the JSON in your question:
if let jsonData = jsonStr.data(using: .utf8) {
if let quiz = try? JSONDecoder().decode(Quiz.self, from: jsonData) {
for question in quiz.quizzes {
print("(question.id) "(question.question)"")
}
}
}
Output
1 "Can't solve this :("
2 "Someone help pls"
answered Nov 17 at 10:50
Stephan Schlecht
1,622188
1,622188
add a comment |
add a comment |
V. Garcia is a new contributor. Be nice, and check out our Code of Conduct.
V. Garcia is a new contributor. Be nice, and check out our Code of Conduct.
V. Garcia is a new contributor. Be nice, and check out our Code of Conduct.
V. Garcia is a new contributor. Be nice, and check out our Code of Conduct.
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%2f53349721%2fswift-3-cant-retrieve-the-info-after-downloading-this-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
i have no resource to write swift 2 , the answer is in NSCoding
– Sh_Khan
Nov 17 at 9:58
Have you actually tried this? Personally I would go for the more generic format [String:Any] and have runtime check the structure of the data as it is parsed
– Spads
Nov 17 at 10:11
Your data type should be
[String:[[String:Any]]]
– Sviatoslav Yakymiv
Nov 17 at 10:56