Bilal
Bilal

Reputation: 69

SWIFT parsing issues

I am new to SWIFT.

I want to parse Json in SWIFT but when I add jsonresult as an array values added without UTF8StringConversion but when I convert then I am unable to add it to array in sWIFT

how can I parse this data and get array of dictionary with UTF8StringEncoding.

[
  {
    "id": "56556523d035cafbdb5dae19",
    "club_name": "Tallkrogens IF",
    "image_url": "https://s3-eu-west-1.amazonaws.com/openratio-client-sportway/561ceb8b68dd3.jpeg"
  },
  {
    "id": "56556523d035cafbdb5dae1a",
    "club_name": "Team Kedde FK",
    "image_url": "testing url"
  },
  {
    "id": "56556523d035cafbdb5dae1b",
    "club_name": "Tekniska Högskolan FC",
    "image_url": "testing url"
  },
  {
    "id": "56556523d035cafbdb5dae1c",
    "club_name": "Tensta IF",
    "image_url": "testing url"
  }

]

Upvotes: 0

Views: 61

Answers (1)

kabiroberai
kabiroberai

Reputation: 2913

You need to use NSJSONSerialization for this. Assuming your string is in a variable called jsonString, try the following.

let data = jsonString.dataUsingEncoding(NSUTF8StringEncoding)
guard let jsonData = try? NSJSONSerialization.JSONObjectWithData(data ?? NSData(), options: .MutableContainers), arr = jsonData as? NSArray else {
    //Handle possible errors here
    return
}
//Do stuff with the arr variable
print(arr)

Upvotes: 1

Related Questions