i know this is very basic question that i am gonna ask but i need to ask how can i access data which is dictionary that is getting from server.
我知道这是一个很基本的问题,我要问,但是我需要问一下,我怎样才能访问从服务器得到的字典。
here is my response
这是我的回答
JSON: {
message = "The email must be a valid email address.";}
now i want to do "po" in console log so what to write after in po statement
现在,我想在控制台日志中做“po”,以便在po语句之后写些什么。
Thanks
谢谢
1 个解决方案
#1
1
All you need type
你所需要的类型
po yourResponseAsDictionary["message"]
UPDATED
更新
Sorry I was thinking you already converted it.
对不起,我以为你已经改了。
If you are not using anything like SwiftyJSON or ObjectMapper to parse your json data then you can do just the following. But I would recommend to use some lib to convert response directly to your model
如果您没有使用swift json或ObjectMapper来解析json数据,那么您可以执行以下操作。但是我建议使用一些lib直接将响应转换为模型
let yourResponseFromNetwork = "{ \"country_code\" : \"+9\", \"user_id\" : 123456}"
if let data = yourResponseFromNetwork.data(using: String.Encoding.utf8) {
do {
if let dic = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String:Any] {
let countryCode = dic["country_code"]
let userId = dic["user_id"]
}
} catch {
print("Error occurred")
}
}
#1
1
All you need type
你所需要的类型
po yourResponseAsDictionary["message"]
UPDATED
更新
Sorry I was thinking you already converted it.
对不起,我以为你已经改了。
If you are not using anything like SwiftyJSON or ObjectMapper to parse your json data then you can do just the following. But I would recommend to use some lib to convert response directly to your model
如果您没有使用swift json或ObjectMapper来解析json数据,那么您可以执行以下操作。但是我建议使用一些lib直接将响应转换为模型
let yourResponseFromNetwork = "{ \"country_code\" : \"+9\", \"user_id\" : 123456}"
if let data = yourResponseFromNetwork.data(using: String.Encoding.utf8) {
do {
if let dic = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String:Any] {
let countryCode = dic["country_code"]
let userId = dic["user_id"]
}
} catch {
print("Error occurred")
}
}