无法将jsonArray元素转换为整数 [英] Cannot convert jsonArray element to integer
本文介绍了无法将jsonArray元素转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
do{
let resultJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())
let arrayJSON = resultJSON as! NSArray
let success:NSInteger = arrayJSON["success"] as! NSInteger
if (success == 1 ) ....
json数据是来自服务器的响应,我试图将其转换为整数,但出现对话错误.
json data is the response from the server, i am trying to convert it to integer but i get the conversation error.
推荐答案
这是一个有效的实例(在我的计算机上经过测试)
This is a working exmaple (tested on my machine)
let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
if let error = error {
print(error)
}
if let data = data{
print("data =\(data)")
do{
let resultJSON = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions())
let resultDictionary = resultJSON as? NSDictionary
let success = resultDictionary!["success"]!
let successInteger = success as! Int
print("success = \(success)")
if successInteger == 1 {
print("yes")
}else{
print("no")
}
}catch _{
print("Received not-well-formatted JSON")
}
}
if let response = response {
print("url = \(response.URL!)")
print("response = \(response)")
let httpResponse = response as! NSHTTPURLResponse
print("response code = \(httpResponse.statusCode)")
}
})
task.resume()
响应为:
{ "error_message" : "No User", "success" : 0}
注意
您说您的服务器响应为:
Note
you said that your server responnes as:
{ "error_message" = "No User"; success = 0; }
这不是不是有效的json,您应该更正它以匹配我给您的json
and this is not a valid json, you should correct it to match the json that i gave to you
这篇关于无法将jsonArray元素转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文