如何使用swift

时间:2016-06-28 14:05:51

标签: swift swifty-json

示例json数据是

     [
       {
        "id": "244",
        "name": "PIZZAS",
        "subcategory": [
          {
            "id": "515",
            "name": "MARGARITA",
            "description": "Cheese and Tomato",
            "image": "",
            "icon": "",
            "coupon": "1",
            "order": "1",
            "aname": "",
            "options": "2",
            "item": [
                        {
                           "id": "1749",
                           "name": "9 Inch Thin & Crispy Margarita",
                           "description": "",
                           "price": "3.40",
                           "coupon": "1",
                           "image": "",
                           "options": "2",
                           "order": "1",
                           "addon": "495",
                           "aname": "",
                           "icon": ""
                    }]
             }]
        }]

我的Json数据并不准确。有多个子类别和项目。我只在这里发布样本数据。

我可以将主要项目提取到类模型中。但是如何获取子类别数据以及项目数据。因为它是一个对象数组。 我已经像这样创建了类模型

子类别

     import Foundation
     class SubCategory {
                       var id: Int?
                       var name: String?
                       var desc: String?
                       var image: String?
                       var coupon: Int?
                       var icon: String?
                       var order: Int?
                       var aname: String?
                       var options: Int?
                       var items:Array<AnyObject>?

init(id:Int?,name:String?,desc:String?,image:String?,coupon:Int?,icon:String?,order:Int?,aname:String?,options:Int?,items:Array<AnyObject>?){
    self.id = id
    self.name = name
    self.desc = name
    self.image = image
    self.coupon = coupon
    self.icon = icon
    self.order = order
    self.aname = aname
    self.options = options
    self.items = items

   }
  }

之后我将子类别数据发送到类模​​型。但主要项目数据正在进行中。我知道我在做错事。但我怎样才能进入子类别部分

          //subcategory section
                   var subcategories = [SubCategory]()
                    for(_,content) in json{ ////here instead of json what should be written so that i can get the subcategory value
                        let subcategory = SubCategory(id: Int(content["id"].stringValue),

                          name: content["name"].string,
                          desc: content["desc"].string,
                          image: content["image"].string,
                          coupon: Int(content["coupon"].stringValue),
                          icon: content["icon"].string,
                          order: Int(content["order"].stringValue),
                          aname: content["aname"].string,
                          options: Int(content["options"].stringValue),
                          items:content["items"].arrayObject)
               subcategories.append(subcategory)

                   }
                   for subcategory in subcategories {
                       print(subcategory.name)
                        print(subcategory.desc)
                        print(subcategory.id)

                    }
                    print(subcategories.count)

我想将所有子类别数据以及项目数据放入我的模态类中。如何实施?

1 个答案:

答案 0 :(得分:1)

你只需要来自该JSON数组的第一个元素的“subcategory”对象,不是吗?

for (_, item) in json {
    //do something with item.id, item.name

    for (_, subcategory) in item["subcategory"] {

        let subcategory = SubCategory(
            id: Int(subcategory ["id"].stringValue),
            name: subcategory ["name"].string,
            desc: subcategory ["desc"].string,
            image: subcategory ["image"].string,
            coupon: Int(subcategory ["coupon"].stringValue),
            icon: subcategory ["icon"].string,
            order: Int(subcategory ["order"].stringValue),
            aname: subcategory ["aname"].string,
            options: Int(subcategory ["options"].stringValue),
            items: subcategory ["items"].arrayObject
        )

        subcategories.append(subcategory)
    }

    //...
}

.append很糟糕。在这种情况下,有一个更好的方式:

for (_, item) in json {
    //do something with item.id, item.name

    subcategories = item.1["subcategory"].map{ subcategory in
         return SubCategory(
            id: Int(subcategory ["id"].stringValue),
            name: subcategory ["name"].string,
            desc: subcategory ["desc"].string,
            image: subcategory ["image"].string,
            coupon: Int(subcategory ["coupon"].stringValue),
            icon: subcategory ["icon"].string,
            order: Int(subcategory ["order"].stringValue),
            aname: subcategory ["aname"].string,
            options: Int(subcategory ["options"].stringValue),
            items: subcategory ["items"].arrayObject
        )
    }
}