【问题标题】:Objectmapper Dictionary To Json swift? Swift 2 -> Swift 3Objectmapper Dictionary To Json swift?斯威夫特 2 -> 斯威夫特 3
【发布时间】:2017-02-15 02:22:28
【问题描述】:

我无法将我的 Dictionary json 放到我的“MyOwnClass”中。在 Swift 2 中一切都很好。

我的 Json 结构:

[
    {
        "dataString1" : "value",
        "dataInt1" : 3277
    },
    {
        "dataString1" : "value",
        ... and so on    

我在 Swift 2.x 中使用了 Objectmapper,但现在我想在 Swift 3 中转换所有内容。

我的旧代码:

do {
    let jsonSerilization = try NSJSONSerialization.JSONObjectWithData(json, options: NSJSONReadingOptions.MutableContainers) as! NSArray

    for jsonCount in 0..<jsonSerilization.count {
         let singleData = Mapper<MyOwnClass>().map(jsonSerilization[jsonCount])
         print(singleData.dataString1) //gotValue
    }


} catch {
    ...
    }

现在jsonSerilization[jsonCount] 行是一个字典。在控制台中,我找到了我所有的值。

但我希望这本字典在我的“MyOwnClass”结构中。我无法将我的字典转换为字符串。我试过 dictionary.description 但这个字符串的格式有误。

MyOwnClass 结构并不特别。

class MyOwnClass : Mappable {
 var dataString1: String?
 var dataInt1: Int?

 required init?(map: Map){
     mapping(map: map)
 }

 func mapping(map: Map) {
    dataString1 <- map["dataString1"]
    dataInt1 <- map["dataInt1"]
 }
}

如何在“MyOwnClass”中传输字典中的值? 当然我可以手动完成,但我想要一个自动的解决方案。

我搜索了很多将字典转换为 json 字符串,但没有任何帮助。谢谢

编辑 看来 ObjectMapper 无法解析这种 json 结构(状态:10/2016) 如果我错了,请告诉我。

【问题讨论】:

    标签: ios swift dictionary swift3 objectmapper


    【解决方案1】:

    使用[[String:Any]] 代替NSArrayJSONObjectWithData 在Swift 3 中如下所示。

    let jsonSerilization = try JSONSerialization.jsonObject(with: json, options: []) as! [[String:Any]]
    for dic in jsonSerilization {
         let singleData = Mapper<MyOwnClass>().map(dic)
         print(singleData.dataString1) //gotValue
    }
    

    【讨论】:

      【解决方案2】:

      首先——一如既往——使用 Swift 原生集合类型。您将丢弃重要的类型信息。

      其次,停止使用丑陋的 C 风格 for 循环。

      第三,仅读取 JSON 时根本不需要可变容器。

      let jsonSerialization = try JSONSerialization.jsonObject(with:json, options: []) as! [[String:Any]]
      
      for item in jsonSerialization {
          let singleData = Mapper<MyOwnClass>().map(JSON:item)
          print(singleData.dataString1) //gotValue
      }
      

      编辑:

      对于两个属性,您根本不需要 ObjectMapper。没有库,代码就更少了

      class MyOwnClass  {
        var dataString1: String?
        var dataInt1: Int?
      
        init(JSON: [String:Any]) {
          self.dataString1  = JSON["dataString1"] as? String
          self.dataInt1  = JSON["dataInt1"] as? Int
        }
      }
      

      然后调用它

      let singleData = MyOwnClass(JSON:item)
      

      【讨论】:

      • 是的,我也有这个代码。但我的问题是该项目现在是一个字典,其值为“dataString1”:“value”和“dataInt”:3277。但是我应该如何在我的“MyOwnClass”中传输这些数据。这是我的问题。对不起,我描述得不够完美。
      • 然后将您的映射器更改为适当的键,例如dataString &lt;- map["dataString1"],但考虑该属性仍然是dataString(当然您也可以更改它)。
      • 对不起。这是一个错误。但是这一行 let singleData = Mapper().map(item) 会引发错误。在 Swift 3 中,我必须设置一个前缀: let singleData = Mapper().map(JSONString: item) 但这个不起作用,因为 item 不是字符串。我如何把这个变成一个 json 字符串?
      • 我更新了答案。根据文档,参数标签似乎是JSON。但基本上Objectmapper 两个键/值是矫枉过正。您可以在库不更改两行的情况下做到这一点。
      • 好的,谢谢您的第二意见。那么这不是我的愚蠢。很高兴知道。然后我必须手动修复它。
      猜你喜欢
      • 2017-03-24
      • 1970-01-01
      • 2017-07-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 2016-02-08
      • 2017-11-13
      相关资源
      最近更新 更多