ASP.Net에 구축 된 이전 유형의 웹 서비스가 있습니다. 다음과 같은 기능으로, 내가 ASMX 웹 서비스에서 데이터의 일부 유형을 가져올 수 있었다 :웹 서비스에서 반환 된 json 객체의 유형을 결정할 수 없습니다.
func getJsonData(sql: String, spparamnames: String, spParamValues: String, completeonClosure: @escaping (AnyObject?) ->()) {
let url = URL(string:"http://www.example.com/MyWebService.asmx/GetDataTableAsJson")
var request = URLRequest(url: url!)
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type") // the request is JSON
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Accept") // the expected response is also JSON
request.httpMethod = "POST"
let dictionary = ["sql" : sql, "spparamnames" : spparamnames, "spparamvalues" : spParamValues] //Parameters are here seperated with comma
request.httpBody = try! JSONSerialization.data(withJSONObject: dictionary)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error.debugDescription) // some fundamental network error
return
}
do {
if response != nil {
let myJson = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:AnyObject]
let isCorectJson = JSONSerialization.isValidJSONObject(myJson)
let charcount = (myJson["d"] as? String)?.characters.count
let cc = charcount ?? 0
if isCorectJson == true && cc > 0 {
completeonClosure(myJson as AnyObject?)
)
} else {
let str2 = "Connection Error"
completeonClosure(str2 as AnyObject?)
}
}
} catch let JsonError {
print(JsonError)
}
}
task.resume()
}
내가 스위프트와 쿼리를 실행하고 NSDictionary
로 객체 유형을 캐스트, 내 출력 결과는 다음과 같다 :
getJsonData(sql: "SELECT TOP 3 User_id, LoweredUserName FROM Users", spparamnames: "", spParamValues: "") {
returnJSON in
OperationQueue.main.addOperation {
let mystr = returnJSON as? NSDictionary
print(mystr!)
}
}
는 결과 :
나는 결과가 사전의 일종이라고 생각{
d = "[{\"User_id\":102,\"LoweredUserName\":\"abu alay\"},{\"User_id\":90,\"LoweredUserName\":\"ali es\"},{\"User_id\":95,\"LoweredUserName\":\"alper ay\"}]";
}
, 그러므로 나는 내가 ITE 수없는 배열로 결과를 변환 할 수 없습니다 행 사이의 비율을 계산하고 결과를 효율적으로 사용하십시오. 결과를 읽으려면 어떻게해야합니까? print (returnJSON [0] [ "LoweredUserName"])? 결과의 시작 부분에있는 문자 "d"의 의미는 무엇입니까? 미리 감사드립니다.
이 사전의 "d"키 값은 사전의 배열 인 다른 JSON 객체의 문자열 표현입니다. 가능한 복제본 http://stackoverflow.com/q/37423950/2227743 – Moritz
감사합니다. Eric. 알았다. 문자 "d"는 실제로 사전의 배열입니다. 따라서 의미있는 결과를 반환하려면 다음을 웹 서비스 함수에 추가해야합니다. str = myJson [ "d"] as? 문자열 completeonClosure (AnyObject로서의 str) – user1960968