我是在Xcode中的iOS相当新的,无法弄清楚如何解析此JSON:解析JSON的Xcode
{
"status": 0,
"result": {
"user_guid": 1158,
"token": "kl7859ee0a9kd27b65k344791d3d754e",
"new_user": "true"
}
}
我试图解析“令牌”出来,但无法弄清楚如何。我将如何去解析这个?
我是在Xcode中的iOS相当新的,无法弄清楚如何解析此JSON:解析JSON的Xcode
{
"status": 0,
"result": {
"user_guid": 1158,
"token": "kl7859ee0a9kd27b65k344791d3d754e",
"new_user": "true"
}
}
我试图解析“令牌”出来,但无法弄清楚如何。我将如何去解析这个?
的Objective-C:
NSString *jsonText = @"Your json string";
NSData *jsonData = [jsonText dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:nil];
NSString *token = [[dic valueForKey:@"result"] valueForKey:@"token"];
雨燕3.0:
let jsonText = "Your json string"
if let jsonData = jsonText.data(using: String.Encoding.utf8) {
do {
let dic = try JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) as! NSDictionary
let token:String = (dic.value(forKey: "result") as! NSDictionary).value(forKey: "token") as! String
NSLog("%@", token)
} catch {
debugPrint(error.localizedDescription)
}
}
运行它时,“未解决的标识符的使用我收到此错误在此之前‘NSJSONReadingMutableContainers’ – user2423476
请检查我更新的答案。 –
它工作!感谢您的帮助 – user2423476
从终点获取数据。然后,它是这样的:
if let dict = <insert response object> as? Dictionary<String, AnyObject> {
if let token = dict["token"] as? String {
// Set the string to the value in app.
}
}
设值= response.value( forKeyPath:“result.token”)as! NSString的
你需要获取结果,然后取令牌,但你需要分析这个使用nsjsonserialization metthod –