2015-09-07 49 views
6

從web服務(API)解碼JSON當我得到錯誤:斯威夫特JSON錯誤:無法投型 '__NSDictionaryM' 的價值 '的NSArray'

Could not cast value of type '__NSDictionaryM' (0x1037ad8a8) to 'NSArray' (0x1037ad470). 

我的代碼:

var kGetURL = "http://bitnami.local/cscart_demo/api/users" 

//var kGetURL = "http://localhost/fendy/getjson.php" 

var json : Array<AnyObject> = [] 

override func viewDidLoad() { 
    super.viewDidLoad() 
    start() 
} 

func getData(data : NSData){ 
    //error at this line : 
    json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! Array<AnyObject> 
    //error 
    tableView.reloadData() 
} 

func start(){ 
    var url : NSURL = NSURL(string: kGetURL)! 
    var data : NSData = NSData(contentsOfURL: url)! 
    getData(data) 
} 

如果我將網址更改爲http://localhost/fendy/getjson.php,其工作非常好。

我得到錯誤,如果我的網址是http://bitnami.local/cscart_demo/api/users

從web服務http://localhost/fendy/getjson.php響應:

[{"id":"17","Name":"KFC","Message":"awesome"}, 
{"id":"18","Name":"McDonald","Message":"good"}, 
{"id":"23","Name":"Burger King","Message":"tasty"}, 
{"id":"38","Name":"Pizza hut","Message":"yummy"}, 
{"id":"39","Name":"Steak","Message":"very Delicious"}] 

從web服務http://bitnami.local/cscart_demo/api/users響應:

{"users": 
[{"user_id":"4","user_login":"user_4","is_root":"N","timestamp":"1441608048","user_type":"C","status":"A","firstname":"","lastname":"","email":"[email protected]","company":"","company_id":"1","company_name":"Simtech"}, 
{"user_id":"3","user_login":"customer","is_root":"N","timestamp":"1441604240","user_type":"C","status":"A","firstname":"Customer","lastname":"Customer","email":"[email protected]","company":"Simtech","company_id":"1","company_name":"Simtech"}, 
{"user_id":"1","user_login":"admin","is_root":"Y","timestamp":"1441604240","user_type":"A","status":"A","firstname":"John","lastname":"Doe","email":"[email protected]","company":"Your company","company_id":"0","company_name":null}], 
"params":{"page":1,"items_per_page":"10","sort_order":"asc","sort_by":"name","sort_order_rev":"desc","total_items":"3"}} 

我認爲它的風格是一樣的,但爲什麼不與網址http://bitnami.local/cscart_demo/api/users一起工作。任何人都可以幫忙

回答

4

bitnami響應以{開頭,因此它是JSON object,對應於NSDictionary。另一個以[開頭,表示一個數組。

您需要的json類型更改爲Dictionary<String, AnyObject>,和反序列化如下:

json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! Dictionary<String, AnyObject> 
+0

?這是可能的?在tableView CellforRowIndexPath我改變:var info:AnyObject? = json [indexpath.row]? – fendy

0

你的方法是鑄造JSON結果爲數組。它可以正常工作,並返回一個表示爲JSON的數組,但它不適用於返回字典的URL,而不是以JSON表示的數組。

雖然返回值的「風格」看起來相同,但第二個是單項目字典。你可能想要的是從中提取"users"元素,它是一個數組。

如果你不知道你所得到的這兩個網址,你可以嘗試用as?兩種風格,而不是投的as!:如果我使用JSON數據類型的NSDictionary

let tmp : AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) 
if let arr = tmp as? Array<AnyObject> { 
    json = arr 
} else if dict = tmp as? [String: AnyObject] { 
    json = dict["users"] as! Array<AnyObject> 
} else { 
    // Handle an error: the input was unexpected 
} 
tableView.reloadData() 
+0

那麼,鍵入Data變量json必須NSDictionary?我按照你的例子得到錯誤: 如果讓arr?數組 {//在一個條件中的變量綁定需要一個初始化程序 – fendy

+0

@fendy你是對的,我忘了做任務。現在就試試。 – dasblinkenlight

相關問題