Swift JSON错误:无法将'__NSDictionaryM'类型的值转换为'NSArray' [英] Swift JSON error : Could not cast value of type '__NSDictionaryM' to 'NSArray'
问题描述
从webservice(API)解码JSON时出现错误:
when decoding JSON from webservice(API) i get error :
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 ,它的工作非常好。
if i change url to http://localhost/fendy/getjson.php, its working so nice.
如果我的网址是 http://bitnami.local/cscart_demo/api/,我会收到错误用户
来自webservice的响应 http:// localhost / fendy / getjson.php :
Response from webservice 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"}]
来自webservice http://bitnami.local的回复/ cscart_demo / api / users :
Response from webservice 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":"fendy.w@mvig.net","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":"customer@example.com","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":"robby@mvig.net","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"}}
我认为它的风格是相同的,但为什么不使用url http://bitnami.local/cscart_demo/api/users 。有人可以提供帮助吗?
i think it's Style is same, but why not working with url http://bitnami.local/cscart_demo/api/users . anyone can help?
推荐答案
bitnami响应以 {
开头因此它是 JSON对象,对应于 NSDictionary
。另一个以 [
开头,表示数组。
The bitnami response starts with a {
and it is therefore a JSON object, which corresponds to an NSDictionary
. The other one starts with [
which indicates an array.
您需要更改<$ c $的类型c> json 到 Dictionary< String,AnyObject>
,并按如下方式反序列化:
You need to change the type of json
to Dictionary<String, AnyObject>
, and deserialize as follows:
json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! Dictionary<String, AnyObject>
这篇关于Swift JSON错误:无法将'__NSDictionaryM'类型的值转换为'NSArray'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!