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 '['.










share|improve this question







New contributor




V. Garcia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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















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 '['.










share|improve this question







New contributor




V. Garcia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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













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 '['.










share|improve this question







New contributor




V. Garcia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question







New contributor




V. Garcia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




V. Garcia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




V. Garcia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 17 at 9:00









V. Garcia

61




61




New contributor




V. Garcia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





V. Garcia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






V. Garcia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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












  • 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












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"





share|improve this answer





















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


    }
    });






    V. Garcia is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    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

























    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"





    share|improve this answer

























      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"





      share|improve this answer























        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"





        share|improve this answer












        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"






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 17 at 10:50









        Stephan Schlecht

        1,622188




        1,622188






















            V. Garcia is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            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.















             


            draft saved


            draft discarded














            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





















































            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

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy