av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

在Swift中優(yōu)雅地處理JSON

swiftyJSON的使用十分的簡單:

10年積累的成都網(wǎng)站制作、做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有平果免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

典型的NSURLSessionTask抓取Twitter的API將產(chǎn)生dataFromNetwork: NSData!:

你首先應(yīng)該做的事情是初始化JSONValue:

 
 
 
  1. let json = JSONValue(dataFromNetwork) 

JSONValue是一個(gè)枚舉類型表示一個(gè)典型的JSON數(shù)據(jù)結(jié)構(gòu)。

你能使用subscripts檢索不同的值從原始的JSONValue中,像這樣:

 
 
 
  1. let userName:JSONValue = json[0]["user"]["name"] 

注意userName仍然是一個(gè)JSONValue。那怎樣得到一個(gè)字符串呢?

你能用.string屬性得到JSON數(shù)據(jù)表示的真正值。

 
 
 
  1. let userNameString = userName.string! 

對(duì)每一種JSON類型, JSONValue都提供了一種屬性檢索它:

 
 
 
  1. var string: String? 
  2. var number: NSNumber? 
  3. var bool: Bool?  
  4. var array: Array
  5. var object: Dictionary

注意每一種屬性都是一個(gè)Optional值。這是因?yàn)镴SON數(shù)據(jù)能包含任何它定義的有效類型。

因此,建議的方式是用Optional綁定檢索值:

 
 
 
  1. if let name = userName.string{ 
  2.     //This could avoid lots of crashes caused by the unexpected data types 
  3.   
  4. if let name = userName.number{ 
  5.     //As the value of the userName is Not a number. It won't execute. 

.number屬性產(chǎn)生一個(gè)NSNumber值,在Swift中這通常不是很有用。你能用.double或者.integer得到一個(gè)Double值或者一個(gè)Int值。

 
 
 
  1. if let intValue = numberValue.integer{ 
  2.     count += intValue 

枚舉(Enumeration)

在Swift中JSONValue實(shí)際上是一個(gè)枚舉:

 
 
 
  1. enum JSONValue { 
  2.   
  3.     case JNumber(NSNumber) 
  4.     case JString(String) 
  5.     case JBool(Bool) 
  6.     case JNull 
  7.     case JArray(Array
  8.     case JObject(Dictionary
  9.     case JInvalid(NSError) 
  10.   

你可以使用一個(gè)switch子句去更有效地獲取值:

 
 
 
  1. let json = JSONValue(jsonObject) 
  2. switch json["user_id"]{ 
  3. case .JString(let stringValue): 
  4.     let id = stringValue.toInt() 
  5. case .JNumber(let numberValue): 
  6.     let id = numberValue.integerValue 
  7. default: 
  8.     println("ooops!!! JSON Data is Unexpected or Broken") 

下標(biāo)(Subscripts)

注意,在JSON中一個(gè)數(shù)組結(jié)構(gòu)被包裝成intoArray,它意味著數(shù)組里的每一個(gè)元素都是一個(gè)JSONValue。甚至你從JSONValue中取出一個(gè)數(shù)組,你仍然可以使用基本的屬性去獲取元素的值:

 
 
 
  1. if let array = json["key_of_array"].array{ 
  2.     if let string = array[0].string{ 
  3.         //The array[0] is still a JSONValue! 
  4.     } 

對(duì)象也是一樣。因此,推薦的方式是訪問每一個(gè)數(shù)組和對(duì)象時(shí)使用JSONValue的下標(biāo)。

 
 
 
  1. if let string = json["key_of_array"][0].string{ 
  2.   

實(shí)際上,你可以用下標(biāo)訪問一個(gè)JSONValue,還不用擔(dān)心運(yùn)行時(shí)錯(cuò)誤導(dǎo)致的崩潰:

 
 
 
  1. let userName = json[99999]["wrong_key"] 

如果你使用推薦的方式去取數(shù)據(jù),它是安全的:

 
 
 
  1. if let userName = json[99999]["wrong_key"]["name"].string{ 
  2.     //It's always safe 

打印

JSONValue遵守Printable協(xié)議.所以很容易在原始字符串中得到JSON數(shù)據(jù):

 
 
 
  1. let json = JSONValue(dataFromNetwork) 
  2. println(json) 
  3. /*You can get a well printed human readable raw JSON string: 
  4.       { 
  5.         "url": { 
  6.           "urls": [ 
  7.             { 
  8.               "expanded_url": null, 
  9.               "url": "http://bit.ly/oauth-dancer", 
  10.               "indices": [ 
  11.                 0, 
  12.                 26 
  13.               ], 
  14.               "display_url": null 
  15.             } 
  16.           ] 
  17.        } 
  18. */ 

如果你不想打印出來,你可以使用.description屬性來得到上述字符串。

 
 
 
  1. let printableString = json.description 

調(diào)試與錯(cuò)誤處理

要是JSON數(shù)據(jù)出錯(cuò)或者我們錯(cuò)誤地檢索數(shù)據(jù),那會(huì)怎么樣呢?你可以使用if語句來測試:

 
 
 
  1. let json = JSONValue(dataFromNetworking)["some_key"]["some_wrong_key"]["wrong_name"] 
  2. if json{ 
  3.   //JSONValue it self conforms to Protocol "LogicValue", with JSONValue.JInvalid stands for false and others stands true 

如果我們嘗試使用錯(cuò)誤的鍵值或索引來訪問數(shù)據(jù),description屬性會(huì)高數(shù)你KeyPath在哪里出錯(cuò)了.

 
 
 
  1. let json = JSONValue(dataFromNetworking)["some_key"]["some_wrong_key"]["wrong_name"] 
  2. if json{ 
  3.   
  4. } else { 
  5.   println(json) 
  6.   //> JSON Keypath Error: Incorrect Keypath "some_wrong_key/wrong_name" 
  7.   //It always tells you where your key went wrong 
  8.   switch json{ 
  9.   case .JInvalid(let error): 
  10.     //An NSError containing detailed error information  
  11.   } 

后記

 SwiftyJSON的開發(fā)將會(huì)發(fā)布在Github, 請(qǐng)持續(xù)關(guān)注后續(xù)版本。


當(dāng)前題目:在Swift中優(yōu)雅地處理JSON
本文URL:http://uogjgqi.cn/article/dhhopss.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們?cè)谖⑿派?4小時(shí)期待你的聲音

解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流