如果我只需要 Json 中的一个字符串,那么处理 Alamofire 响应的最佳做法是什么

What is the best practice of handling Alamofire response If I only need one string from Json

我对处理 Alamofire 的响应有疑问 json,我需要一些建议。我使用 SwiftyJSON 来解析 json。返回json就是这样;

        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` 的东西并将所有这些值映射到模型 class 中以在我需要的地方使用?谢谢。

Is it an acceptable way to use closure and pass this value like that?

是的,这是处理异步调用的唯一正确方法

Even If i need only TransactionState , still I should use something like ObjectMapper` and map all these values in a model class

不,你不需要它们,也不需要 Codable,使用 swifty json 这里也是最短的路径 json序列化可以完成这项工作