Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use .count #14

Closed
pketh opened this issue Jul 1, 2014 · 3 comments
Closed

How to use .count #14

pketh opened this issue Jul 1, 2014 · 3 comments

Comments

@pketh
Copy link

pketh commented Jul 1, 2014

Hi,
I'm using SwiftyJSON in a project where I'm using

println(boardsJSON["boards"])

to return the following JSON

[
  {
    "idOrganization":null,
    "id":"5325aa22252e6dbe7bc6bf24",
    "pinned":true,
    "name":"Space Tiger",
    "closed":false
  },
  {
    "idOrganization":null,
    "id":"53a6dd5e29abc0630ab317fa",
    "pinned":true,
    "name":"Work Stuff",
    "closed":false
  }
]

I can dive down the tree using something like

println(boardsJSON["boards"][0]["name"])

But if I wanted to iterate over all the names in my JSON how would I get the count of objects in the array?

@lingoer
Copy link
Member

lingoer commented Jul 2, 2014

You can use the .array property to cast a JSONValue in to an Array.
And the .string property can cast a JSONValue in to String

if let boardsArray = boardsJSON["boards"]{
  let countOfArray = boardsArray.count //Here you got your count
}
//If you are sure about the stability of your data struct, you can also use:
for board in boardsJSON["boards"].array!{
  let name = board["name"].string!
}

Hope this help.

@pketh
Copy link
Author

pketh commented Jul 2, 2014

Thanks

@pketh pketh closed this as completed Jul 2, 2014
@jpmcglone
Copy link

Is there a way to have it support Array(obj) for conversion?

nghuiqin pushed a commit to nghuiqin/SwiftyJSON that referenced this issue Sep 20, 2016
Renamed the Kitura-Build repo to Package-Builder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants