gpt4 book ai didi

ios - 从 SOAP api 调用检索 json

转载 作者:行者123 更新时间:2023-11-30 11:46:32 25 4
gpt4 key购买 nike

我是 SOAP 新手。当我进行 api 调用时,我使用 postman 得到了以下响应(XML 中的 json 对象)。

<string xmlns="http://*******/">
{"Id":0,"NumberType":null,"Number":null,"FullName":null}
</string>

我尝试使用 Alamofire 和 SwiftyJSON。

  Alamofire.request(common.getURL(), method: .post,parameters: ["strId": "1"], encoding: JSONEncoding.default, headers: NetworkingUtil.getAlamoFireHeader()).responseJSON { (Response) in
let error = Response.result.error
let req = Response.request
let res = Response.response
let json = Response.data
if error != nil {
if req != nil && res != nil {
print(req!)
print(res!)
}
}else{
var parsedjson : JSON!;
do {
parsedjson = try JSON(data : json!)
print(parsedjson!)
} catch {
print(error)
}
}
}

输出

    {
"d" : "{\"Id\":0,\"NumberType\":null,\"Number\":null,\"FullName\":null}"
}

如何从响应中提取 json 对象?是否有适用于 iOS swift 的标准库,我可以使用 Alamofire 来实现此目的吗?我无法快速找到一个好的解决方案。提前致谢

最佳答案

我曾经做过类似的事情,只是没有使用 AlamoFire 或 Postman。这是我获取 JSON 的方法:

func downloadJSON(_ completion:  @escaping (_ result: AnyObject?, _ error: NSError?) -> Void ) -> URLSessionDataTask {
// build url and submit for data in order

let url = Client.URLFromParameters(Client.Constants.Parse.Scheme, Client.Constants.Parse.Host, Client.Constants.Parse.Path, withPathExtension: Client.Constants.Methods.StudentLocation , withQuery: "?order=-updatedAt")

return Client.sharedInstance().doAllTasks(url: url, task: "GET", jsonBody: "", truncatePrefix: 0, completionHandlerForAllTasks: completion)

}

//Client.sharedInstance().doAllTask​​s() 执行 URLTask 并通过完成处理程序返回数据。重要的部分发生在下面列出的convertDataWithCompletionHandler()中:

private func convertDataWithCompletionHandler(_ data: Data, completionHandlerForConvertData: (_ result: AnyObject?, _ error: NSError?) -> Void) {

var parsedResult: AnyObject! = nil
do {
parsedResult = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as AnyObject
} catch {
let userInfo = [NSLocalizedDescriptionKey : "Could not parse the data as JSON: '\(data)'"]
completionHandlerForConvertData(nil, NSError(domain: "convertDataWithCompletionHandler", code: 1, userInfo: userInfo))
}
completionHandlerForConvertData(parsedResult, nil)
}

然后读取我这样做的对象:

func getLocations(completed: @escaping (_ error: NSError?) -> ()) {
// try to download data and scrub it
// on success store in array of locations
downloadJSON({ (result, error) in
if error != nil {
print(error?.localizedDescription)
completed(error)
} else {
let locationsDict = result?["results"] as! [[String : Any]]
for item in locationsDict {
// MARK: REVISIT
var newLocation = StudentLocation(studentLocation: item)
if let dstring = item["updatedAt"] as? String {
newLocation.updatedAt = self.getDate(date: dstring)
//self.locations.append(newLocation)
StudentDataSource.sharedInstance.studentData.append(newLocation)
}
}
}
print(result)
self.sortLocations {
//print(self.locations.description)
}
completed(nil)
})
}

我认为您需要使用的部分是

let locationsDict = result?["results"] as! [[String : Any]]

关于ios - 从 SOAP api 调用检索 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48773656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com