如何使用迅速

问题描述:

样品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) 

我希望所有的子类别数据以及项目数据到我的模态类。如何实施?

你只是想从该JSON数组的第一个元素的“子类别”对象,不是吗?

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 
     ) 
    } 
} 
+0

,但如果我有这么多的子类别,然后如何可以根据这里计数 –

+0

只有我发出JSON的样本数据数量,我得到的所有子类别的数据。我的数据不仅仅是一个子类和一个项目。 –

+0

你应该让你的问题更清楚。 – Alexander