【问题标题】:Use ObjectMapper to parse Dictionary within a Dictionary Swift使用 ObjectMapper 在 Dictionary Swift 中解析 Dictionary
【发布时间】:2017-01-06 05:42:37
【问题描述】:

我的旧 JSON 结构是这样的:

{
"parameters": {
"customerId": 9,
"from": "2014-06-05T14:00:00",
"until": "2014-06-05T15:00:00",
"km": 20,
"insurance": false
},
"estimatesPerCategory": {
  "2": {
  "timeCost": 5,
  "kmCost": 6,
  ... 
 }

为了解析 JSON,我使用 ObjectMapper。 这段代码效果很好:

if let myObject = Mapper<CostEstimateResult>().map(JSONObject: JSON) {

}

CostEstimateResult 类如下所示。

class CostEstimateResult : NSObject, Mappable {

var parameters:CostEstimateParameters?
var estimatesPerCategory:[String: CostEstimate]? // CostEstimate.id -> CostEstimate


override init() {}

required convenience init?(map: Map) {
    self.init()
    self.mapping(map: map)
}

 func mapping(map: Map) {
    estimatesPerCategory <- map["estimatesPerCategory"]
    parameters <- map["parameters"]
 }
}

最后但并非最不重要的是我的 CostEstimate 类的estimatesPerCategory

class CostEstimate : NSObject, Mappable {

var timeCost:NSNumber?
...
override init() {}

required convenience init?(map: Map) {
    self.init()
    self.mapping(map: map)
}

func mapping(map: Map) {
    timeCost <- map["timeCost"]
    ...
    }
}

这是有效的。 CostEstimateResult 中的映射器将调用 CostEstimates 中的映射器,我得到我的解析数据。

现在我更改了 JSON 结构:

{
"parameters": {
//same like before
},
"estimatesPerCategory": {
 "2": {
  "total": {
    "preTax": {
      "value": 1,
      "text": "1,00 €"
    },
    "configTax": false
  }, 
 "3": {
     //same...
   }
 }
}

在我看来,基本结构没有任何变化。 estimatesPerCategory 仍然是同一个字典。 estimatesPerCategory后面的数据改了,好吧。

问题是,当我调用映射器...Mapper&lt;CostEstimateResult&gt;().map... 时,将解析参数,但不会解析costEstimates。 CostEstimate 中的 func mapping 不再调用

长话短说:estimatesPerCategory 中的 estimatesPerCategory 的类型似乎随着 json 结构而变化。我不知道我需要哪种类型。

【问题讨论】:

    标签: ios json swift dictionary objectmapper


    【解决方案1】:

    现在有了新的 JSON 结构,您的 Class CostEstimate 结构将被更改,并且需要创建一个新的类 PreTax

    class CostEstimateResult : NSObject, Mappable {
    
        var parameters:CostEstimateParameters?
        var estimatesPerCategory:[String: CostEstimate]? // CostEstimate.id -> CostEstimate
    
        override init() {}
    
        required convenience init?(map: Map) {
            self.init()
            self.mapping(map: map)
        }
    
        func mapping(map: Map) {
            estimatesPerCategory <- map["estimatesPerCategory"]
        }
    }
    
    
    class CostEstimate : NSObject, Mappable {
        var total:totalTax?
    
        override init() {}
    
        required convenience init?(map: Map) {
            self.init()
            self.mapping(map: map)
        }
    
        func mapping(map: Map) {
            total <- map["total"]
        }
    }
    
    class totalTax : NSObject, Mappable {
        var preTax:PreTax?
        var configTax:Bool?
    
        override init() {}
    
        required convenience init?(map: Map) {
            self.init()
            self.mapping(map: map)
        }
    
        func mapping(map: Map) {
            preTax <- map["preTax"]
            configTax <- map["configTax"]
        }
    }
    
    class PreTax : NSObject, Mappable {
    
        var value:NSNumber?
        var text:String?
    
        override init() {}
    
        required convenience init?(map: Map) {
            self.init()
            self.mapping(map: map)
        }
    
        func mapping(map: Map) {
            value <- map["value"]
            text <- map["text"]
        }
    }
    

    【讨论】:

    • 感谢回复,但它不起作用,因为没有调用CostEstimate的映射器,我不知道为什么。为什么我必须解析 PreTax?我必须先解析 Total,不是吗?
    • 你是如何测试它的?我复制了你的代码,我理解结构。看起来不错,但我的主要问题是 estimatesPerCategory &lt;- map["estimatesPerCategory"] 这一行不执行 CostEstimate 类。我不知道为什么
    • 我创建了一个数组并转换为 JSON 并使用 ObjectMapper 进行解析,我能够访问所有值。尝试删除 NSObjectoverride init() {}
    • 对我也不起作用。你会把你的测试项目发给我还是放在github上?我将在这里查看并发布(如果我找到)解决方案。我的邮件是:snoopybig@gmx.de
    • 检查这个临时文件(48小时后过期):expirebox.com/download/4354f00862abaca70b4140c82081ceec.html
    猜你喜欢
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2014-04-05
    • 2012-03-01
    • 2017-02-15
    • 2015-10-18
    相关资源
    最近更新 更多