【问题标题】:SwiftyJSON conversion to multiple string arraysSwiftyJSON 转换为多个字符串数组
【发布时间】:2018-08-10 03:05:29
【问题描述】:

鉴于以下示例 JSON

{
    "filters": [ 
      { "name" : "First Type",
        "types" : ["md", "b", "pb"]},
      { "name" : "Second Type",
        "types" : ["pt", "ft", "t"]},
      { "name" : "Third Type",
        "types" : ["c", "r", "s", "f"]
      }
    ],
    "jobs": [
        { "title":"f",
          "description" : "descrip text",
          "criteria":[ "md", "ft", "s" ],
          "img" : "www1"
        },
        { "title":"boa",
          "description" : "a description",
          "criteria":[ "b", "pb", "f", "ft" ],
          "img" : "www2"
        },
        { "title":"BK",
          "description" : "something here",
          "criteria":[ "md", "pt", "ft", "b", "s" ],
          "img" : "www3"
        }
    ]
 }

(使用 Alamofire 创建响应) 让 responseJSON : JSON = JSON(response.result.value!)

1) 我正在尝试将它们转换为两个字符串数组。一个数组: let filter = [String : [String]] 和另一个数组用于作业。我该怎么做? (又名给男人一条鱼)以下是一些示例代码 sn-ps,但没有一个甚至接近工作。

  let filterCategories = responseJSON["filters"].arrayValue.map({
          $0["name"].stringValue
  })

for (key,subJson):(String, JSON) in responseJSON["filters"] {
    let object : filterObject = filterObject(category: key, list: subJson.arrayValue.map({ $0.stringValue }))

  }

2) 我如何学习如何正确使用它? (又名教人钓鱼)我一直在阅读文档(https://github.com/SwiftyJSON/SwiftyJSON),但我很难理解它。我猜最终答案将使用 .map、.stringValue 和 .arrayValue。最终,尽管我试图避免大量不必要或无法管理的代码。

【问题讨论】:

标签: ios arrays swift swifty-json


【解决方案1】:

Swift 4 提供开箱即用的 JSON 解析支持 - 可能从 Ultimate Guide to JSON Parsing with Swift 4 之类的东西开始

根据你可用的结构,我扔进了一个游乐场并使用了......

// I was loading the JSON from a file within the Playground's Resource folder
// But basically, you want to end up with a reference to Data
let filePath = Bundle.main.path(forResource:"Source", ofType: "json")
let data = FileManager.default.contents(atPath: filePath!)

struct Filter: Codable {
    let name: String;
    let types: [String];
}

struct Job: Codable {
    let title: String;
    let description: String;
    let criteria: [String];
    let img: String;
}

struct Stuff: Codable {
    let filters: [Filter];
    let jobs: [Job];
}

let decoder = JSONDecoder();
let stuff = try! decoder.decode(Stuff.self, from: data!)

print("Filter:")
for filter in stuff.filters {
    print(filter.name)
    for type in filter.types {
        print(" - \(type)")
    }
}
print("Jobs:")
for job in stuff.jobs {
    print(job.title)
    print(job.description)
    print(job.img)
    for type in job.criteria {
        print(" - \(type)")
    }
}

解析结果

【讨论】:

  • 感觉像巫毒魔法。 stuff.filters 和 stuff.jobs 包含我正在寻找的数据。
  • 是的,是的,但它(非常)可喜的变化是以前必须做的每一件事,尽管 SwiftyJSON 很棒,但这要好得多 - 恕我直言
  • 对通过此问题/答案的任何人的旁注:对我来说一瞬间并不明显,但是您的 JSON 的“结构”需要与所涉及的结构相匹配。例如,如果我选择将 JSON 文件中的“jobs”更改为“abc”,则结构也需要更改(“let jobs”变为“let abc”)。
  • 另外,我在处理构建嵌套数组时使用了stackoverflow.com/a/47681156/290196 这个答案。
  • @Alex 您“可能”能够编写自己的“编码器”,这在Ultimate Guide to JSON Parsing with Swift 4 的“自定义键名”部分进行了演示;)
【解决方案2】:

您可以实现Codable 协议来解析响应。使用你的 json 响应而不是这个

let url = Bundle.main.url(forResource: "data", withExtension: "json")
let data = NSData(contentsOf: url!)

我将它用于游乐场进行测试。

struct Root: Codable {
   let jobs: [Jobs]
   let filters: [Filters]

private enum CodingKeys: String, CodingKey {
    case jobs = "jobs"
    case filters = "filters"
 }
}

struct Filters: Codable {
    let name: String?
    let typees: String?
}

struct Jobs: Codable {
     let title: String?
     let description: String?
     let criteria: [String]?
     let img: String? 
}

let url = Bundle.main.url(forResource: "data", withExtension: "json")
let data = NSData(contentsOf: url!)


do {
      let root = try JSONDecoder().decode(Root.self, from: data as! Data)

      if let name = root.jobs.first?.title {
      print(name)
      }


} catch let error as NSError {

   print(error.description)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2020-06-06
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 2019-08-04
    相关资源
    最近更新 更多