日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
在Swift中優(yōu)雅地處理JSON

swiftyJSON的使用十分的簡單:

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

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

你首先應該做的事情是初始化JSONValue:

 
 
 
  1. let json = JSONValue(dataFromNetwork) 

JSONValue是一個枚舉類型表示一個典型的JSON數(shù)據(jù)結構。

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

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

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

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

 
 
 
  1. let userNameString = userName.string! 

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

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

注意每一種屬性都是一個Optional值。這是因為JSON數(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)生一個NSNumber值,在Swift中這通常不是很有用。你能用.double或者.integer得到一個Double值或者一個Int值。

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

枚舉(Enumeration)

在Swift中JSONValue實際上是一個枚舉:

 
 
 
  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.   

你可以使用一個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") 

下標(Subscripts)

注意,在JSON中一個數(shù)組結構被包裝成intoArray,它意味著數(shù)組里的每一個元素都是一個JSONValue。甚至你從JSONValue中取出一個數(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.     } 

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

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

實際上,你可以用下標訪問一個JSONValue,還不用擔心運行時錯誤導致的崩潰:

 
 
 
  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 

調試與錯誤處理

要是JSON數(shù)據(jù)出錯或者我們錯誤地檢索數(shù)據(jù),那會怎么樣呢?你可以使用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 

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

 
 
 
  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ā)將會發(fā)布在Github, 請持續(xù)關注后續(xù)版本。


當前文章:在Swift中優(yōu)雅地處理JSON
當前URL:http://www.5511xx.com/article/dhhopss.html