ios - 如何在 Swift 5 中将 JSON 数据存储到模型类数组中
问题描述
嗨,我是 swift ios 的初学者,我的要求是必须显示对表列表的 Json 响应,我从 Web 服务得到响应,响应如下所示
我的要求是如何将该模型类映射到 Array 以及如何在 tableList 中显示它们可以帮助我吗
我的回复
[{
"id": 6,
"products": {
"items": [{
"status": 1,
"custom_attributes": [
{
"attribute_code": "short_description",
"value": "short_description"
},
{
"attribute_code": "meta_title",
"value": "meta_title"
},
{
"attribute_code": "meta_keyword",
"value": "meta_keyword"
}
]
},
{
"status": 1,
"custom_attributes": [
{
"attribute_code": "short_description",
"value": "short_description"
},
{
"attribute_code": "meta_title",
"value": "meta_title"
},
{
"attribute_code": "meta_keyword",
"value": "meta_keyword"
}
]
}
]
}
},
{
"id": 6,
"products": {
"items": [{
"status": 1,
"custom_attributes": [
{
"attribute_code": "short_description",
"value": "short_description"
},
{
"attribute_code": "meta_title",
"value": "meta_title"
},
{
"attribute_code": "meta_keyword",
"value": "meta_keyword"
}
]
},
{
"status": 1,
"custom_attributes": [
{
"attribute_code": "short_description",
"value": "short_description"
},
{
"attribute_code": "meta_title",
"value": "meta_title"
},
{
"attribute_code": "meta_keyword",
"value": "meta_keyword"
}
]
}
]
}
}
]
我试过这个
class DashboardProduct: NSObject {
class dashProductBook : NSObject {
static var products : [Product]!
//MARK: Init
@discardableResult init(dictionary: [String:Any]){
dashProductBook.products = [Product]()
if let dashProductArray = dictionary["products"] as? [[String:Any]]{
for dic in dashProductArray{
let value = Product(dictionary: dic)
dashProductBook.products.append(value)
}
}
}
//MARK: Delete
static func deleteFromModel()
{
products = nil
}
//MARK: Return data
static func toDictionary() -> [String:Any]
{
var dictionary = [String:Any]()
if products != nil{
var dictionaryElements = [[String:Any]]()
for productElement in products {
dictionaryElements.append(productElement.toDictionary())
}
dictionary["products"] = dictionaryElements
}
return dictionary
}
}
class Product : NSObject{
var items : [Items]!
init(dictionary: [String:Any]) {
items = [Items]()
if let regionArray = dictionary["items"] as? [[String:Any]]{
for dic in regionArray{
let value = Items(dictionary: dic)
items.append(value)
}
}
}
func toDictionary() -> [String:Any]
{
var dictionary = [String:Any]()
if items != nil{
var dictionaryElements = [[String:Any]]()
for itemElement in items {
dictionaryElements.append(itemElement.toDictionary())
}
dictionary["items"] = dictionaryElements
}
return dictionary
}
}
class Items : NSObject{
var custom_Attr : [Custom_Attribute]!
init(dictionary: [String:Any]) {
custom_Attr = [Custom_Attribute]()
if let custom_attributesArray = dictionary["custom_attributes"] as? [[String:Any]]{
for dic in custom_attributesArray{
let value = Custom_Attribute(dictionary: dic)
custom_Attr.append(value)
}
}
}
func toDictionary() -> [String:Any]
{
var dictionary = [String:Any]()
if custom_Attr != nil{
var dictionaryElements = [[String:Any]]()
for customElement in custom_Attr {
dictionaryElements.append(customElement.toDictionary())
}
dictionary["custom_attributes"] = dictionaryElements
}
return dictionary
}
}
class Custom_Attribute : NSObject{
var key: String?
var value: String?
init(dictionary: [String: Any]) {
self.key = dictionary["key"] as? String
self.value = dictionary["value"] as? String
}
func toDictionary() -> [String:Any]
{
var dictionary = [String:Any]()
if key != nil{
dictionary["attribute_code"] = key
}
if value != nil{
dictionary["value"] = value
}
return dictionary
}
}
}
但它给了我错误“无法将类型'NSArray'的值转换为强制类型'[String:Any]'”
解决方案
这是一个使用 ObjectMapper Library 的简单解决方案,您可以参考此类
// 使用 ObjectMapper 库
import ObjectMapper
class jsonModel : Mappable {
var id = 0
var products: productsDataModel?
required init?(map: Map) {
//
}
func mapping(map: Map) {
id <- map["id"]
products <- map["products"]
} } class productsDataModel: Mappable {
var items : [itemsDataModel]?
required init?(map: Map) {
//
}
func mapping(map: Map) {
items <- map["items"]
}}class itemsDataModel: Mappable {
var status = 0
var custom_attributes = [custom_attributesDataModel]?
required init?(map: Map) {
//
}
func mapping(map: Map) {
status <- map["status"]
custom_attributes <- map["custom_attributes"]
}}class custom_attributesDataModel: Mappable {
var attribute_code = ""
var value = ""
required init?(map: Map) {
//
}
func mapping(map: Map) {
attribute_code <- map["attribute_code"]
value <- map["value"]
}}
推荐阅读
- python - 为什么元素评估为假?
- php - array_intersect 并同时匹配多个值
- python - 对熊猫数据框进行列表理解
- typescript - 有没有办法在导入语句中放置类型断言?
- javascript - 无法使用字符串作为参数从 PHP 调用 JavaScript 函数
- javascript - 尝试将语义 UI 中的按钮放入代码中时,不断收到错误消息。错误是错误:无法在未安装的组件上找到节点
- cmake - 使用现有目标作为新但相关目标的基础
- python - 优化代码以查找下一个素数
- apache-spark - 将 SQL 逻辑“OR EXISTS(子查询)”转换为 Spark 半连接
- python - 对使用多个模块进行日志记录的疑问