【发布时间】:2019-04-26 19:27:23
【问题描述】:
我的ResponseString如下,
SUCCESS:
{"code":200,"shop_detail":{"name":"dad","address":"556666"},
"shop_types : [{"name":"IT\/SOFTWARE","merchant_type":"office"}]}
我的带头的Get请求代码如下,
func getProfileAPI() {
let headers: HTTPHeaders = [
"Authorisation": AuthService.instance.tokenId ?? "",
"Content-Type": "application/json",
"Accept": "application/json"
]
print(headers)
let scriptUrl = "http://haitch.igenuz.com/api/merchant/profile"
if let url = URL(string: scriptUrl) {
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = HTTPMethod.get.rawValue
urlRequest.addValue(AuthService.instance.tokenId ?? "", forHTTPHeaderField: "Authorization")
urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
urlRequest.addValue("application/json", forHTTPHeaderField: "Accept")
Alamofire.request(urlRequest)
.responseString { response in
debugPrint(response)
print(response)
if let result = response.result.value // getting the json value from the server
{
print(result)
let jsonData1 = result as NSString
print(jsonData1)
let name = jsonData1.object(forKey: "code") as! [AnyHashable: Any]
print(name)
// var data = jsonData1!["shop_detail"]?["name"] as? String
} }
}
当我试图获取“名称”的值时,它得到了'[<__nscfstring> valueForUndefinedKey:]:此类不符合键值编码的键码。请指导我获取名称、地址的值..??????
【问题讨论】:
标签: ios json string xcode swift4