parse json

28 views
Skip to first unread message

Akram Moncer

unread,
Mar 25, 2015, 3:45:47 PM3/25/15
to swift-l...@googlegroups.com
Hi All 

How can i parse this type of json to get the list of phone numbers :
{ "phoneNumbers": [ "(555) 564-8583","(555) 564-8584"
] }


thanks

Dennis W

unread,
Mar 26, 2015, 1:10:20 AM3/26/15
to swift-l...@googlegroups.com
The json is a dictionary that contains a key called phoneNumbers and an array of values (the actual phone numbers) , you access the contents of a dictionary using the key.

let phoneNumbers = jsonDictionary["phoneNumbers"]

Jens Alfke

unread,
Mar 26, 2015, 1:50:16 AM3/26/15
to Dennis W, swift-l...@googlegroups.com
If you’re asking how to parse it, the short answer is to use NSJSONSerialization. Or you could read any of dozens of tutorials on how to parse JSON in Swift.

—Jens

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.
To post to this group, send email to swift-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/9ff3ec50-e330-4fab-a5d6-41b8b0513397%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Akram Moncer

unread,
Mar 26, 2015, 3:40:44 AM3/26/15
to swift-l...@googlegroups.com
thank you all for your responses :

my exact problem is :

using this expression : let phoneNumbers = jsonDictionary["phoneNumbers"]

i get an optional containing all the phone numbers list but when i try to get specific one exp: phoneNumbers[0] i get an exception "bad access"

Brent Royal-Gordon

unread,
Mar 26, 2015, 4:08:51 AM3/26/15
to Akram Moncer, swift-l...@googlegroups.com
using this expression : let phoneNumbers = jsonDictionary["phoneNumbers"]

i get an optional containing all the phone numbers list but when i try to get specific one exp: phoneNumbers[0] i get an exception "bad access"

You'll generally need to cast elements to their proper types as you retrieve them from a JSON dictionary:

    jsonDictionary["phoneNumbers"] as! [String]    // or as? if you want to detect and handle ill-formed JSON

Jeremy Pereira

unread,
Mar 26, 2015, 1:06:38 PM3/26/15
to Akram Moncer, swift-l...@googlegroups.com

> On 26 Mar 2015, at 07:40, Akram Moncer <akram....@gmail.com> wrote:
>
> thank you all for your responses :
>
> my exact problem is :
>
> using this expression : let phoneNumbers = jsonDictionary["phoneNumbers"]
>
> i get an optional containing all the phone numbers list but when i try to get specific one exp: phoneNumbers[0] i get an exception "bad access"

You'll have to

a) unwrap the optional
b) cast the unwrapped phoneNumbers to an array of strings.

Then you can subscript it.


>
> Le mercredi 25 mars 2015 20:45:47 UTC+1, Akram Moncer a écrit :
> Hi All
>
> How can i parse this type of json to get the list of phone numbers :
> {
> "
> phoneNumbers":
> [
>
> "(555) 564-8583","(555) 564-8584"
> ]
> }
>
>
>
> thanks
>
> --
> You received this message because you are subscribed to the Google Groups "Swift Language" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.
> To post to this group, send email to swift-l...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/e37ac6d6-e5ce-4538-ba3f-3bf5b9ae8ad3%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages