func callAddWithPOST(Name mname:String, PhoneNo mphone:String, Email memail:String, Comment mcomments:String)
{
var names = [String]()
let login = ["countryId":"1"]
print("Your Result is : = \(login)")
let url = NSURL(string: "http://photokeeper.mgtcloud.co.uk/commonwebservice.asmx/getStateList")!
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: url)
do
{
let auth = try NSJSONSerialization.dataWithJSONObject(login, options: .PrettyPrinted)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.HTTPMethod = "POST"
request.HTTPBody = auth
let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
let responseString = String(data:data!, encoding: NSUTF8StringEncoding)
let validresponseString = "\(responseString!)"
print(validresponseString)
let badJsonString = "This really isn't valid JSON at all"
let jsonData = validresponseString.dataUsingEncoding(NSUTF8StringEncoding)!
let badJsonData = badJsonString.dataUsingEncoding(NSUTF8StringEncoding)!
do
{
let parsed = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.AllowFragments)
let otherParsed = try NSJSONSerialization.JSONObjectWithData(badJsonData, options: NSJSONReadingOptions.AllowFragments)
}
catch let error as NSError
{
print("Done.")
}
})
task.resume()
}
catch
{
print("Error")
}
}
我正在寻找一种从此方法中获取所需数据的解决方案,我想从此字符串数据中显示StateID
和StateName。我无法将NSData
中的字符串“responseString”转换为将此字符串作为NSData
来获取所需的记录。请问有谁帮我解决这个问题。
答案 0 :(得分:0)
如果你想从json获取key的值,你应该使用以下代码将json转换为Dictionary:
JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject]
NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String:AnyObject]
如果我在downvote之前误解了某些内容,请在评论中告诉我,谢谢。