2015-05-15 16 views
1

我需要解析JSON數組中的10個數組並將其存取。我可以通過REST API獲取數據並在控制檯上打印出來,但我不知道如何實際保存它以處理。在swift中解析一個雙JSON編碼數組並訪問它

JSON陣列結構:

[[lol_id, lol_time, lol_uid, lol_username, lol_type, lol_url, lol_title, lol_score, lol_comments, lol_badge], 
[lol_id, lol_time, lol_uid, lol_username, lol_type, lol_url, lol_title, lol_score, lol_comments, lol_badge], 
[lol_id, lol_time, lol_uid, lol_username, lol_type, lol_url, lol_title, lol_score, lol_comments, lol_badge], 
[lol_id, lol_time, lol_uid, lol_username, lol_type, lol_url, lol_title, lol_score, lol_comments, lol_badge], 
[lol_id, lol_time, lol_uid, lol_username, lol_type, lol_url, lol_title, lol_score, lol_comments, lol_badge], 
[lol_id, lol_time, lol_uid, lol_username, lol_type, lol_url, lol_title, lol_score, lol_comments, lol_badge], 
[lol_id, lol_time, lol_uid, lol_username, lol_type, lol_url, lol_title, lol_score, lol_comments, lol_badge], 
[lol_id, lol_time, lol_uid, lol_username, lol_type, lol_url, lol_title, lol_score, lol_comments, lol_badge], 
[lol_id, lol_time, lol_uid, lol_username, lol_type, lol_url, lol_title, lol_score, lol_comments, lol_badge], 
[lol_id, lol_time, lol_uid, lol_username, lol_type, lol_url, lol_title, lol_score, lol_comments, lol_badge]] 

那些陣列基本上返回整數和字符串。

如何將這些保存爲相同樣式(數組中的數組)並訪問它們?

這裏是我的了代碼直到如今

@IBAction func buttonLoad(sender: AnyObject) { 

    // Setting up the URL 
    let myUrl = NSURL(string: "http://hugelol.com/api/front.php"); 

    // Request 
    let request = NSMutableURLRequest(URL:myUrl!); 
    request.HTTPMethod = "POST"; 

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { 
     data, response, error in 

     if error != nil 
     { 
      println("error=\(error)") 
      return 
     } 

     // Prints server response 
     println("response = \(response)") 

     // Prints the double array 
     let responseString = NSString(data: data, encoding: NSUTF8StringEncoding) 
     println("responseString = \(responseString)") 
    } 
    task.resume() 
} 

我已經嘗試了許多不同的方法,但有很大的麻煩了解究竟是如何做到這一點。我也嘗試過swiftyJSON,但我仍然不知道如何將數據保存到快速數組中。我正在將我的頭撞到牆上。謝謝你的閱讀。

+0

我有_SwiftyJSON_ 這樣的結果讓'海峽=「[大聲笑,大聲笑,Lol],[Lol,Lol,Lol]]''; 'let data = JSON(str)'; 'println(data); // [[Lol,Lol,Lol],[Lol,Lol,Lol]] –

回答

0

這部分代碼應該提供給您預期的結果

.... 

    // Setting up the URL 
    let myUrl = NSURL(string: "http://hugelol.com/api/front.php"); 

    // Request 
    let request = NSMutableURLRequest(URL:myUrl!); 
    request.HTTPMethod = "POST"; 

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { 
     data, response, error in 

     if error != nil 
     { 
      println("error=\(error)") 
      return 
     } 

     if let jsonString = JSON(data: data).rawString() as String? { 
      if let dataFromString = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) { 
       let json = JSON(data: dataFromString) 
// uncomment necessary access method     
//     for (index: String, subJson: JSON) in json { 
//      println("Arr = \(subJson)") 
//     } 
// OR 
//     println(json[0][0]) 

      } 
     } 
    } 

    task.resume()   

....

+0

謝謝,這會將我打印出實際的陣列。 但是,我正在尋找一個'json [0] [0]'表單中的單個值,例如第一個數組中的第一個項目。 – Luxuspunch

+0

我明白你的意思了。實際上,響應不是JSON數據,它只是包含值的數組。 JSON有{「key」:「value」}格式 –

+0

有趣。我認爲我的數據可能有問題:當我嘗試打印某些東西時,例如: 'let json = JSON(data:data) let items = json [0] [0] println(items )' 它剛剛返回'null'或'nil'。我會研究如何解決這個問題,但我覺得這可能是一個很大的痛苦。任何建議來緩解我的痛苦? – Luxuspunch

0

嘗試使用NSJSONSerialization。它將解析JSON並返回一個NSObject樹。

if let result: AnyObject = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: nil) {...} 
+0

如何使用返回的NSObject樹「結果」? – Luxuspunch