Swift 如果我只需要Json中的一个字符串,那么处理Alamofire响应的最佳实践是什么
我有一个关于处理Alamofire json响应的问题,我需要一些建议。我使用SwiftyJSON解析json。返回的json是这样的Swift 如果我只需要Json中的一个字符串,那么处理Alamofire响应的最佳实践是什么,swift,alamofire,Swift,Alamofire,我有一个关于处理Alamofire json响应的问题,我需要一些建议。我使用SwiftyJSON解析json。返回的json是这样的 ResultCode = "122"; ResultText = "The content of this sample could not be recognized."; TransactionDetails = { IsComplete = 0; TransactionId =
ResultCode = "122";
ResultText = "The content of this sample could not be recognized.";
TransactionDetails = {
IsComplete = 0;
TransactionId = 9398;
Transactionstate = OnGoing;
Transactiontype = Authentication;
};
VoiceDetails = {
DiscardedSpeech = 1;
ProcessResult = None;
SpeechResult = BadContentSpeech;
TotalSpeech = 1;
};
};
}
如果我只需要json中的TransactionState
。使用闭包并像那样传递这个值是可以接受的方法吗
if let strState : String = swiftyJsonVar["AuthVoicePrintData"]["TransactionDetails"]["TransactionState"].string
{
completionHandler(strState)
return
}
completionHandler("Something went wrong")
或者,即使我只需要TransactionState
,我仍然应该使用类似ObjectMapper`的东西,并将模型类中的所有这些值映射到我需要的地方?多谢各位
使用闭包并像那样传递这个值是可以接受的方法吗
if let strState : String = swiftyJsonVar["AuthVoicePrintData"]["TransactionDetails"]["TransactionState"].string
{
completionHandler(strState)
return
}
completionHandler("Something went wrong")
是的,这是处理异步调用的唯一正确方法
即使我只需要TransactionState,我仍然应该使用类似ObjectMapper的东西,并将所有这些值映射到一个模型类中
不,你不需要它们,也不需要编码,使用快速json这里是最短的路径,jsonSerialization也可以做这项工作你研究过使用
.isEmpty
来检查字符串是否存在值吗?是的,我知道isEmpty,你建议用它来代替if-let条件吗?谢谢您是否正在检查该值是否存在或如何传递该值?再读一遍,问题似乎很模糊。对不起,我的英语不好。实际上是在寻找传递值的正确方法。“if let”条件检查值是否存在,如果存在,则使用闭包传递值,如果不存在,则传递字符串“出错”。我在问传递值的方式是否可以接受,或者最好创建模型数据并使用objectmapper之类的工具进行映射。谢谢