-2

hi i am trying to get the son response from the server and the following is the response i get from server while using postman

{
  "stories": [
{
  "image_url": "http://storybox.dk/wp-content/uploads/2016/11/Hansa.jpg",
  "name": "New story",
  "story_key": "ahJlfnN0b3J5Ym94LWJhY2tlbmRyEgsSBVN0b3J5GICAgIC8oYIKDA"
},
{
  "image_url": "http://storybox.dk/wp-content/uploads/2016/11/Hansa.jpg",
  "name": "Story of Foo the fox.",
  "story_key": "ahJlfnN0b3J5Ym94LWJhY2tlbmRyEgsSBVN0b3J5GICAgID4woQKDA"
}]
}

but when i try to get the values inside the stories tag as array it gets only one value as
self.StoryListArr = (reponsedict.value(forKey: "stories") as? NSArray)!

 (
   {
    "image_url" = "http://storage.googleapis.com/storybox-backend.appspot.com/5649391675244544/Screenshot_from_2017-03-16_113338.png";
    name = "Story of Foo";
    "story_key" = ahJlfnN0b3J5Ym94LWJhY2tlbmRyEgsSBVN0b3J5GICAgID4woQKDA;
   }
)

please advice how to deal this

when i try to print response dict it shows only one value in it

{
stories =     (
            {
        "image_url" = "http://storage.googleapis.com/storybox-backend.appspot.com/5649391675244544/Screenshot_from_2017-03-16_113338.png";
        name = "Story of Foo";
        "story_key" = ahJlfnN0b3J5Ym94LWJhY2tlbmRyEgsSBVN0b3J5GICAgID4woQKDA;
    }
);
}
5
  • try reponsedict["stories"] as! [[String:Any]] Commented Mar 22, 2017 at 6:54
  • Use swifty json. It's much easier. Commented Mar 22, 2017 at 6:55
  • I would recommend using ObjectMapper. Here is an example Commented Mar 22, 2017 at 6:56
  • hi i tried to print responsedict and it shows only one value inside the stories tag, i had added it in the question Commented Mar 22, 2017 at 7:01
  • I think problem is about your api. check it again about return data. Commented Mar 22, 2017 at 7:56

1 Answer 1

1

There is no reason to use NSArray in Swift. Simply cast the object to [Dictionary].

if let stories = responsedict["stories"] as? [Dictionary<String,String>] {
    print(stories) 
}
Sign up to request clarification or add additional context in comments.

9 Comments

when i try to print the response dict it shows only one value in it print(reponsedict) { stories = ( { "image_url" = "http://storage.googleapis.com/storybox-backend.appspot.com/5649391675244544/Screenshot_from_2017-03-16_113338.png"; name = "Story of Foo"; "story_key" = ahJlfnN0b3J5Ym94LWJhY2tlbmRyEgsSBVN0b3J5GICAgID4woQKDA; } ); }
Try printing responsedict["stories"] to make sure that you have more than one value in the response in swift
Is the request in postman the exact same as the one in your code?
responsedict["stories"] prints ( { "image_url" = "http://storage.googleapis.com/storybox-backend.appspot.com/5649391675244544/Screenshot_from_2017-03-16_113338.png"; name = "Story of Foo"; "story_key" = ahJlfnN0b3J5Ym94LWJhY2tlbmRyEgsSBVN0b3J5GICAgID4woQKDA; } ) contains only one value
Are you sure that the request is the exact same as the one you did in Postman?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.