如何解析字典值并在swift中传递给类模态?

时间:2016-07-28 10:45:26

标签: arrays json swift dictionary

我的JSON数据

{
 "addon_items" : [
                     {
                      "aname" : "",
                      "id" : "2588",
                      "name" : "Plain Nan",
                      "order" : "1",
                      "aid" : "259",
                      "Sub_Add_Items" : "",
                      "icon" : "",
                      "status" : "1",
                      "next" : "0",
                      "price" : "0.60"
                     },
                     {
                      "aname" : "",
                      "id" : "2589",
                      "name" : "Pitta Bread",
                      "order" : "2",
                      "aid" : "259",
                      "Sub_Add_Items" : "",
                      "icon" : "",
                      "status" : "1",
                      "next" : "0",
                      "price" : "0.00"
                    }

                   ],

 "addon" : {
             "description" : "Please choose your Nan bread",
             "aname" : "",
             "id" : "259",
             "icon" : "",
             "limit" : "1",
             "special_addon" : "",
             "next" : "165"
           }
 }

我创建了三个名为AddOnResponse,AddOn,AddOnItems的类模型,如下所示:

AddOnResponse类模型

class AddOnResponse {

var addon: [String: AnyObject]?
var addonitems: Array<AnyObject>?

init(addon:[String: AnyObject]?,addonitems: Array<AnyObject>?){
    self.addon = addon
    self.addonitems = addonitems
 }
}

AddOn班级模型

class AddOn {


var id: Int?
var icon: String?
var desc: String?
var limit: Int?
var next: Int?
var aname: String?
var specialaddon: Int?

init(id: Int?,icon: String?,desc: String?,limit: Int?,next: Int?,aname: String?,specialaddon: Int?){

    self.id = id
    self.icon = icon
    self.desc = desc
    self.limit = limit
    self.next = next
    self.aname = aname
    self.specialaddon = specialaddon

  }
 }

AddOnItems类模型

class AddOnItems {


var id: Int?
var aid: Int?
var name: String?
var price: Int?
var order: Int?
var status: Int?
var next: Int?
var aname: String?
var subaddItems: Int?
var icon: String?

init(id: Int?,aid: Int?,name: String?,price: Int?,order: Int?,status: Int?,next: Int?,aname: String?,subaddItems: Int?,icon: String?){
    self.id = id
    self.aid = aid
    self.name = name
    self.price = price
    self.order = order
    self.status = status
    self.next = next
    self.aname = aname
    self.subaddItems = subaddItems
    self.icon = icon
   }
 }

现在我使用Alamofire获取我的JSON数据,但是当使用对象将数据传递给addon模型时,我获得了nil值。

    var addonResponses = [AddOnResponse]()

    Alamofire.request(.GET, myAddOnUrl)
        .validate()
        .responseJSON
        {   response in
            switch response.result
            {
            case .Success:
                if let value = response.result.value{
                    let json = JSON(value)
                    print(json)
                    print(json["addon"].arrayValue)

                  let addOnRes = AddOnResponse(addon: json["addon"].object as? [String : AnyObject],
                                 addonitems: json["addon_items"].arrayObject)
              print(addOnRes.addon)

addonaddonitems数据来自类模型。现在,当我将数据传递给Addon模态时,它获得了nil ..

                for (_,content) in json["addon"]{

                        let addon = AddOn(id: Int(content["id"].stringValue),
                                          icon: content["icon"].string,
                                          desc: content["description"].string,
                                          limit: Int(content["limit"].stringValue),
                                          next: Int(content["next"].stringValue),
                                          aname: content["aname"].string,
                                          specialaddon: Int(content["special_addon"].stringValue)
                                         )
                            print(addon.desc) /// nil value
                     }

所以如何解析插件所有数据并传递给类模型..

0 个答案:

没有答案