swift,Alamofire,SwiftyJSON:如何解析arrayObject

时间:2017-05-31 23:21:24

标签: ios swift parsing swifty-json

我不知道如何使用SwiftyJSON.JSON制作数组 我可以使用SwiftyJSON.JSON解析struct但我无法解析对象数组。 请帮帮我。

JSON

{
  "result": {
    "list": [
      {
        "categoryId": 1,
        "categoryNm": "main_1",
        "subCategoryList": [
          {
            "categoryId": 2,
            "categoryNm": "sub_1_1"
          },
          {
            "categoryId": 4,
            "categoryNm": "sub_1_2"
          },
          {
            "categoryId": 5,
            "categoryNm": "sub_1_3"
          },
          {
            "categoryId": 6,
            "categoryNm": "sub_1_4"
          },
          {
            "categoryId": 28,
            "categoryNm": "sub_1_5"
          }
        ]
      },
      {
        "categoryId": 7,
        "categoryNm": "main_2",
        "subCategoryList": [
          {
            "categoryId": 9,
            "categoryNm": "sub_2_1"
          },
          {
            "categoryId": 10,
            "categoryNm": "sub_2_2"
          },
          {
            "categoryId": 11,
            "categoryNm": "sub_2_3"
          }
        ]
      }
    ]
  }
}

Model.swift

struct Model {
    public let list: Array<Category>
    public init?(json: JSON) {
        self.list = json["list"].arrayObject as! Array<Category>
    }
}

struct Category {
    public let categoryId: NSInteger
    public let categoryNm: NSString
    public let subCategoryList: Array<SubCategory>
    public init?(json: JSON) {
        self.categoryId = json["categoryId"].intValue as NSInteger
        self.categoryNm = json["categoryNm"].stringValue as NSString
        self.subCategoryList = json["subCategoryList"].arrayObject as! Array<SubCategory>
    }
}

struct SubCategory {
    public let categoryId: NSInteger
    public let categoryNm: NSString
    public init?(json: JSON) {
        self.categoryId = json["categoryId"].intValue as NSInteger
        self.categoryNm = json["categoryNm"].stringValue as NSString
    }
}

ViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()
    ...
    switch response.result {
        case .success(let value):
            let json = JSON(value)
            let resultModel = Model.init(json: json["result"])
        case .failure(let error):
            print(error)
        }

我不知道如何使用SwiftyJSON.JSON制作数组 我可以使用SwiftyJSON.JSON解析struct但我无法解析对象数组。 请帮帮我。

1 个答案:

答案 0 :(得分:3)

以下代码将 my.json 文件解析为Categories和SubCategories数组

声明categories数组

var categories = [Category]()

打开文件并创建JSON对象

let path = Bundle.main.path(forResource: "my", ofType: "json")!
let jsonData = NSData.init(contentsOfFile: path)
let json = JSON(data: jsonData! as Data)

创建categories并将它们附加到数组

for categoryArray in json["result"]["list"].array! {
    guard let category = Category(json: categoryArray) else { continue }
    categories.append(category)
}
print(categories)

声明Category结构如下

struct Category {
    public let categoryId: NSInteger
    public var subCategories = [SubCategory]()
    public init?(json: JSON) {
        self.categoryId = json["categoryId"].intValue as NSInteger
        self.categoryNm = json["categoryNm"].stringValue as NSString
        for subCategory in json["subCategoryList"].array! {
            subCategories.append(SubCategory(json: subCategory)!)
        }
    }
}