【问题标题】:SWIFT parsing issuesSWIFT 解析问题
【发布时间】:2015-11-28 15:14:22
【问题描述】:

我是 SWIFT 的新手。

我想在 SWIFT 中解析 Json,但是当我将 jsonresult 添加为未添加 UTF8StringConversion 的数组值时,但是当我转换时,我无法将其添加到 sWIFT 中的数组中

如何解析这些数据并使用 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"
  }

]

【问题讨论】:

  • 您必须提供您使用的代码,否则我们如何调试?

标签: arrays json swift utf-8


【解决方案1】:

您需要为此使用NSJSONSerialization。假设您的字符串位于名为 jsonString 的变量中,请尝试以下操作。

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)

【讨论】:

  • @EricD。抱歉,我从我的应用程序中复制了代码。我会改的。
  • @EricD。固定的。我不小心把它变成了dict,因为我在我的应用程序中使用了字典,并且习惯于这样称呼它。
猜你喜欢
  • 2015-06-09
  • 2015-09-28
  • 2021-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多