do {
// JSON 문자열을 데이터로 변환
guard let jsonData = result.data(using: .utf8) else {
print("Invalid JSON string")
return
}
// JSON 데이터를 파싱하여 객체로 변환
guard let jsonArray = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [[String: Any]] else {
print("Failed to parse JSON data")
return
}
// 배열의 각 요소에 접근하여 필요한 정보 추출
for json in jsonArray {
if let result = json["result"] as? String,
let resultMsg = json["resultMsg"] as? String {
// 필요한 정보 출력 또는 다른 작업 수행
print("Result: \\(result)")
print("Result Message: \\(resultMsg)")
} else {
print("Missing required keys in JSON object")
}
}
//배열이 아닐경우
if let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any] {
// "CNT" 키에 대한 값 가져오기
if let cntValue = jsonObject["CNT"] as? String {
print("CNT 값: \\(cntValue)")
}
}
} catch {
print("Error parsing JSON: \\(error.localizedDescription)")
}