【问题标题】:How can I convert `JSON.Element` to `JSON` with "SwiftyJSON"?如何使用“SwiftyJSON”将 `JSON.Element` 转换为 `JSON`?
【发布时间】:2018-02-05 04:16:19
【问题描述】:

我想像下面那样做。如何使用“SwiftyJSON”将JSON.Element 转换为JSON

public func convert() -> String? {
    let json :JSON = ["Animals" : ["dog","cat"], "Shop":"Tokyo"]
    let animals = json["Animals"]
    let repezentativeAnimal = animals.first
    repezentativeAnimal!["Shop"] = json["Shop"] // Value of tuple type 'JSON.Element' (aka '(String, JSON)') has no member 'subscript'

    // I want to return String below
    //  ["dog":"---", "Shop":"Tokyo"]
    return repezentativeAnimal.rawString() // error : Value of type 'JSON.Element?' (aka 'Optional<(String, JSON)>') has no member 'rawString'
}

【问题讨论】:

  • 我可能会在这方面为您提供帮助,但您能否更好地解释您想要实现的目标?返回这样的字符串:["dog":"---", "Shop":"Tokyo"] 完全没有意义
  • 你说I want to return String ["dog":"---", "Shop":"Tokyo"] 但即使在修复你的代码后它也不会返回这个,它只会返回“狗”。你的问题不清楚。如果您将let repezentativeAnimal = animals.first 替换为var repezentativeAnimal = animals.first!.1,您可以看到这一点...

标签: swift swift4 swifty-json


【解决方案1】:

pod 安装

pod 'Alamofire-SwiftyJSON'

并导入您的 VC

导入 SwiftyJSON

导入 Alamofire_SwiftyJSON

【讨论】:

  • 请解释你的答案。
  • 安装 pod 文件然后导入 SwiftyJSON 并导入你的 ViewController 的 Alamofire_SwiftyJSON
猜你喜欢
  • 2016-08-12
  • 2023-04-05
  • 1970-01-01
  • 2016-03-02
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多