Commit 04252921 authored by David黄金龙's avatar David黄金龙

1、数据 解析

2、添加使用方法
parent 379704cb
......@@ -11,8 +11,23 @@ import SmartCodable
struct NetBaseModel: SmartCodable {
var data : SmartAny?
var errorCode : Int?
var errorMessage : String?
var success : Bool?
init() { }
var errorCode : Int = -1
var errorMessage : String = ""
var success : Bool = false
var test: TestModel = TestModel()
var testArr:[TestModel] = []
}
struct TestModel: SmartCodable {
var test1 : String = ""
var test2 : String = ""
var test3 : String = ""
enum CodingKeys: String, CodingKey {
case test1
case test2
case test3 = "test333" //字段 映射
}
}
......@@ -6,6 +6,7 @@
//
import UIKit
import SmartCodable
//智慧服务
class YHIntelligentServiceViewController: YHBaseViewController {
......@@ -14,11 +15,76 @@ class YHIntelligentServiceViewController: YHBaseViewController {
super.viewDidLoad()
initView()
testModelParse()
}
func initView() {
navigationItem.title = "智慧服务"
view.backgroundColor = YHColorWithHex(0xE9E9E9)
}
func testModelParse() {
SmartConfig.debugMode = .verbose
let json = """
{
"data": "balabala Team",
"errorCode": 0,
"errorMessage": "操作成功",
"success":true,
"test": {
"test1": "test1-value",
"test2": "test2-value",
"test333": "test3-value",
},
"testArr":[
{
"test1": "test1-value",
"test2": "test2-value",
"test333": "test3-value",
},
{
"test1": "test1-value",
"test2": "test2-value",
"test333": "test3-value",
},
{
"test1": "test1-value",
"test2": "test2-value",
"test333": "test3-value",
}
]
}
"""
//1. json字符串 转 对象
guard let model = NetBaseModel.deserialize(json: json) else { return }
print("model 是 ==>",model)
print(model)
//2. 对象 转 dictionary
let dic = model.toDictionary()
print("dic 是 ==>",dic as Any)
//3. dictionary 转 string
let jsonString = dic?.jsonString()
print("jsonString 是 ==>",jsonString ?? "")
//4.model 转 string
let jsonString2 = model.toJSONString()
print("jsonString2 是 ==>",jsonString2 ?? "")
//5. dic 转 model
guard let model1 = NetBaseModel.deserialize(dict: dic) else { return }
print("model1 是 ==>",model1)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment