See this article, to know what this article tells you, is an interpretation of the JSON, parsing the code to Baidu can be, OC, Android, JS and so on a lot, but for the swift language of the small white, the information is less, including some can not understand, this is an explanation, To let small white read:
First on the code, this is the most basic get JSON form
Let URL = Nsurl (string: "/http/...")
Let data = NSData (contentsofurl:url!)
if data! = Nil {
Let Dict:nsdictionary = (try! Nsjsonserialization.jsonobjectwithdata (data!, Options:. allowfragments)) as! Nsdictionary//through the system to obtain JSON, the actual development will use the third-party library, such as Alamofire, in order to give small white see (Big God does not read this article), with the most basic. The JSON data that is taken at this point is the dictionary
Print ("Get JSON data")
Print (dict)//Output JSON data
rst = Dict.objectforkey ("rst") as! INT//through key to get value, classic key-value pair form Key-value
ECD = Dict.objectforkey ("ECD") as! Int
}
Understand the most basic JSON, then the actual use, I will not only a key to take value, such as the TableView cell is definitely the form of the array, this time the small white has looked at other people's code to see many times, still do not know other people's cell data from where to obtain, the reason is that, The general practice is to convert the JSON object into a model and then take it from the model object, which translates to a lot of small white waste of time.
Class Consumerdetail:nsobject, Dictmodelprotocol {
var rst:int =-1
var total:int =-1
var rows:[details]?
Class Func Loadconsumerdetail (completion: (Data:consumerdetail, Error:nserror?), Void) {
Let URL = Nsurl (string: "http://183.252.21.19:81/bhys/d/api/?apiType=costDetail&rqsJson=%7B%22cardNo%22:% 229999000002111%22%7d&sign=123456789 ")
Let data = NSData (contentsofurl:url!)
Let path = Nsbundle.mainbundle (). Pathforresource ("Details", Oftype:nil)//This is the local JSON data used at the beginning of the test
Let data = NSData (contentsoffile:path!)
if data! = Nil {
Let Dict:nsdictionary = (try! Nsjsonserialization.jsonobjectwithdata (data!, Options:. allowfragments)) as! Nsdictionary
Let Rspjson = Dict.objectforkey ("Rspjson") as! Nsdictionary
Print ("Get JSON data")
Let Modeltool = dictmodelmanager.sharedmanager//Dictmodelmanager Here is a custom conversion class, such as the following
Let data = Modeltool.objectwithdictionary (Rspjson, cls:consumerDetail.self) as? Consumerdetail//Get conversion class Dictionary to object method to save the object, callback completion return data is the object, error does not do processing
Completion (Data:data, Error:nil)
}
}
static Func customclassmapping (), [string:string]? {
return ["Rows": "\ (details.self)"]//pairing of the model
}
}
Class Details:nsobject {//model, is not and javabean very much like, in fact, Swift and Java quite like, personally feel
var amount:string?
var orgname:string?
var occurtime:string?
var state:string?
}
Here is the Viewcontroller, after obtaining the data object, put it into the model, as follows
Private var detail:consumerdetail?
Override Func Viewdidload () {
Super.viewdidload ()
Self.title = "Consumption Details"
SetTableView ()
Consumerdetail.loadconsumerdetail {(data, error), Void in
Let tmpself = self
Tmpself.detail = Data
}
}
It's easy to get when the cell gets
Func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath), UITableViewCell {
Let cell = Detailcelltableviewcell.cellwithtableview (TableView)
Let goods = detail?. rows! [Indexpath.row]
Cell.details = Goods
Print ("Get Cell")
return cell
}
Custom Transformation Classes
public class Dictmodelmanager {
private static Let Instance = Dictmodelmanager ()
Global Public Unified Access Portal
public class Var Sharedmanager:dictmodelmanager {
return instance
}
Dictionary Turn model
-Parameter dict: Data dictionary
-Parameter CLS: Model class
-Returns: Model object
public func objectwithdictionary (Dict:nsdictionary, Cls:anyclass), Anyobject? {
Let NS = Nsbundle.mainbundle (). infodictionary! ["Cfbundleexecutable"] as! String
Model information
Let infodict = Fullmodelinfo (CLS)
Let Obj:anyobject = (CLS as! Nsobject.type). Init ()
Autoreleasepool {//3. Traverse dictionary to??
For (K, v) in Infodict {
If let Value:anyobject = Dict[k] {
If V.isempty {
if! (Value = = NSNull ()) {
Obj.setvalue (value, forkey:k)
}
} else {
Let type = "\ (value.classforcoder)"
if type = = "Nsdictionary" {
If Let Subobj:anyobject = objectwithdictionary (Value as! Nsdictionary, cls:nsclassfromstring (ns + "." + V)!) {
Obj.setvalue (Subobj, Forkey:k)
}
} else if type = = "Nsarray" {
If Let Subobj:anyobject = Objectswitharray (Value as! Nsarray, cls:nsclassfromstring (ns + "." + V)!) {
Obj.setvalue (Subobj, Forkey:k)
}
}
}
}
}
}
return obj
}
Iosapp's JSON notice