我从API获得以下回复。我需要在swift 3中将响应建模到类中。 JSON中的Api响应如下:
{
"data": [
{
"CategoryName": "אהבה",
"CategoryID": "61",
"CategoryDate": "2016-08-26 02:11:43",
"CategoryImage": "love.png",
"SubCategoryArray": [
{
"SubCategoryName": "אהבה",
"SubCategoryRefID": "61",
"SubCategoryID": "115",
"SubCategoryDate": "2016-08-29 09:25:56",
"TextArray": [
{
"TextID": "103",
"Text": "<p dir=\"rtl\"><span style=\"font-size:48px\"><strong>אוהב</strong> אותך כל כך</span></p>\r\n"
},
如何为subCategory和TextArray子类建模。
我正在使用SwiftyJSON来填充我的模型类
我的模型类如下所示;
class Menu
{
var CategoryName: String?
var CategoryID: String?
var CategoryDate: String?
var CategoryImage: String?
var SubCategoryArray: [SubCategoryArray]?
init(CategoryName: String?, CategoryID: String?, CategoryDate: String?, CategoryImage: String?, SubCategoryArray: [SubCategoryArray]?) {
self.CategoryName = CategoryName
self.CategoryID = CategoryID
self.CategoryDate = CategoryDate
self.CategoryImage = CategoryImage
self.SubCategoryArray = SubCategoryArray
}
}
class SubCategoryArray
{
var SubCategoryName: String?
var SubCategoryRefID: String?
var SubCategoryID: String?
var SubCategoryDate: String?
var TextArray : [TextArray]?
init(SubCategoryName: String?, SubCategoryRefID: String?, SubCategoryID: String?, SubCategoryDate: String?, TextArray: [TextArray]?) {
self.SubCategoryName = SubCategoryName
self.SubCategoryRefID = SubCategoryRefID
self.SubCategoryID = SubCategoryID
self.SubCategoryDate = SubCategoryDate
self.TextArray = TextArray
}
}
class TextArray
{
var TextID: String?
var Text: String?
init (TextID:String? , Text: String? )
{
self.TextID = TextID
self.Text = Text
}
}
我将其存储为
if let json = try? JSON(result){
var menus = [Menu]()
for (_, content) in json["data"] {
let menu = Menu(CategoryName: (content["CategoryName"].string),
CategoryID: content["CategoryID"].string,
CategoryDate: content["CategoryDate"].string,
CategoryImage: content["CategoryImage"],
SubCategoryArray: content["SubCategoryArray"].string)
menus.append(menu)
}
唯一的问题是如何为SubCategory
班级和TextArray
班级做这件事。
答案 0 :(得分:2)
首先,我建议小写变量名。
此外,我会将TextArray
类重命名为Text
,将SubCategoryArray
类重命名为SubCategory
,因为它们不是数组,名称可能会产生误导。< / p>
最后,让我们将subCategoryArray
和textArray
初始化为SubCategory
和Text
的空数组,而不是可选的。这样,无论何时将来使用它,您都不必运行nil
检查,只需检查是否isEmpty
。
您需要做的只是迭代传入的对象,并创建您的数组。请看下面的代码。
class Menu {
var categoryName: String?
var categoryID: String?
var categoryDate: String?
var categoryImage: String?
var subCategoryArray = [SubCategory]()
init(categoryName: String?, categoryID: String?, categoryDate: String?, categoryImage: String?, subCategoryArray: [Any]?) {
self.categoryName = categoryName
self.categoryID = categoryID
self.categoryDate = categoryDate
self.categoryImage = categoryImage
// lets check if SubCategoryArray is not nil
if let subCategoryArray = subCategoryArray as? [[String: AnyObject]] {
// lets iterate on the raw data
subCategoryArray.forEach({ (subCategoryCandidate) in
// Create category
let subcategory = SubCategory(subCategoryName: subCategoryCandidate["SubCategoryName"] as? String,
subCategoryRefID: subCategoryCandidate["SubCategoryRefID"] as? String,
subCategoryID: subCategoryCandidate["SubCategoryID"] as? String,
subCategoryDate: subCategoryCandidate["SubCategoryDate"] as? String,
textArray: subCategoryCandidate["TextArray"] as? [[String: AnyObject]])
// Append to our array
self.subCategoryArray.append(subcategory)
})
}
}
}
// lets rename this class only to SubCategory, because this is not an array at all
class SubCategory {
var subCategoryName: String?
var subCategoryRefID: String?
var subCategoryID: String?
var subCategoryDate: String?
var textArray = [Text]()
init(subCategoryName: String?, subCategoryRefID: String?, subCategoryID: String?, subCategoryDate: String?, textArray: [[String: AnyObject]]?) {
self.subCategoryName = subCategoryName
self.subCategoryRefID = subCategoryRefID
self.subCategoryID = subCategoryID
self.subCategoryDate = subCategoryDate
if let textArray = textArray {
textArray.forEach({ (textArrayCandidate) in
let text = Text(textID: textArrayCandidate["TextID"] as? String, text: textArrayCandidate["Text"] as? String)
self.textArray.append(text)
})
}
}
}
// Rename this to Text, because this is also not an array
class Text {
var textID: String?
var text: String?
init(textID:String? , text: String? ) {
self.textID = textID
self.text = text
}
}
<强> USAGE 强>
let url = URL(string: "YOUR-URL")
let request = URLRequest(url: url!)
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.dataTask(with: request, completionHandler: {(data, response, error) in
if let data = data, let json = try? JSON(data) {
var menus = [Menu]()
for (_, content) in json["data"] {
let menu = Menu(categoryName: (content["CategoryName"].string),
categoryID: content["CategoryID"].string,
categoryDate: content["CategoryDate"].string,
categoryImage: content["CategoryImage"].string,
subCategoryArray: content["SubCategoryArray"].arrayObject)
menus.append(menu)
}
print(menus) // prints 6 items for me
}
});
task.resume()
}