parse json

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

parse json

Akram Moncer
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/eb141c4b-3f3e-471a-8868-873036096451%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: parse json

Dennis W
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"]

On Wednesday, March 25, 2015 at 12:45:47 PM UTC-7, Akram Moncer wrote:
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: parse json

Jens Alfke
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

On Mar 25, 2015, at 10:10 PM, Dennis W <[hidden email]> wrote:

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"]

On Wednesday, March 25, 2015 at 12:45:47 PM UTC-7, Akram Moncer wrote:
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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/DE32E8B0-E06E-4C37-9B94-1E0BD501B328%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: parse json

Akram Moncer
In reply to this post by Akram Moncer
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"

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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/e37ac6d6-e5ce-4538-ba3f-3bf5b9ae8ad3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: parse json

Brent Royal-Gordon-2
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/CAEeRk-aKcdLDhgwBrfUf7S_pkdmQs2MDCbWxd0eON7VO4zC7MQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: parse json

Jeremy Pereira
In reply to this post by Akram Moncer

> On 26 Mar 2015, at 07:40, Akram Moncer <[hidden email]> 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 [hidden email].
> To post to this group, send email to [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/e37ac6d6-e5ce-4538-ba3f-3bf5b9ae8ad3%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/D791B3AA-B346-48EA-A675-8CDD50BEE8C0%40googlemail.com.
For more options, visit https://groups.google.com/d/optout.