I want to parse single value of email from array. I can able to parse image_url, app_name and app_title but don't know how to parse array of value as Text.
I have sample web JSON as show below.
{
"image_url": "file:///Users/developer/Desktop/ios_icon.png",
"app_name": “Developer - iOS App",
"app_title": “Coding is my life”,
“developer”: [
{
"email": “iosapps@gmail.com",
"web”: "https://iosapps.wordpress.com",
"about”: "https://iosapps.wordpress.com/about/“
}
]
}
Here is my tried code:
import SwiftUI
struct JSONParser: View {
@State private var jsonData: JSONData?
var body: some View {
VStack(spacing: 20) {
Spacer()
VStack {
ZStack {
Circle()
.foregroundColor(Color("colorThirdaryLight").opacity(0.1))
.frame(width: 120, height: 120)
.shadow(radius: 4)
AsyncImage(url: URL(string: jsonData?.imageUrl ?? "")) { image in
image
.resizable()
.aspectRatio(contentMode: .fit)
.clipShape(Circle())
.padding()
} placeholder: {
Circle()
.foregroundColor(.secondary)
}
.frame(width: 120, height: 120)
}
Text(jsonData?.appName ?? "")
.bold()
.font(.title3)
.padding()
Text(jsonData?.appTitle ?? "")
.font(.footnote)
.multilineTextAlignment(.center)
Text("\(String(describing: jsonData?.developer))")
.font(.footnote)
.multilineTextAlignment(.center)
.padding(.top)
}
.padding()
.background(Color(red: 242/255, green: 242/255, blue: 247/255))
.cornerRadius(16)
Spacer()
}
.padding()
.task {
do {
jsonData = try await getJSONData()
} catch GHError.invalidURL {
Toast.regular(msg: "URL is invalid!")
} catch GHError.invalidResponse {
Toast.regular(msg: "Invalid response!")
} catch GHError.invalidData {
Toast.regular(msg: "Invalid data!")
} catch {
Toast.regular(msg: "Unexpected error!")
}
}
}
func getJSONData() async throws -> JSONData {
let endPoint = "https:jsonparse.com/xxxxxxxxxxxx"
guard let url = URL(string: endPoint) else {
throw GHError.invalidURL
}
let (data, response) = try await URLSession.shared.data(from: url)
guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
throw GHError.invalidResponse
}
do {
let decoder = JSONDecoder()
let json = try decoder.decode(JSONData.self, from: data)
return json
} catch {
throw GHError.invalidData
}
}
}
struct JSONParser_Previews: PreviewProvider {
static var previews: some View {
JSONParser()
}
}
struct JSONData: Codable {
let imageUrl: String
let appName: String
let appTitle: String
let developer: [Developer]
enum CodingKeys: String, CodingKey {
case imageUrl = "image_url"
case appName = "app_name"
case appTitle = "app_title"
case developer = "developer"
}
}
extension JSONData {
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
imageUrl = try values.decode(String.self, forKey: .imageUrl)
appName = try values.decode(String.self, forKey: .appName)
appTitle = try values.decode(String.self, forKey: .appTitle)
developer = try values.decode([Developer].self, forKey: .developer)
}
}
struct Developer: Codable {
let email: String
enum CodingKeys: String, CodingKey {
case email = "email"
}
}
extension Developer {
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
email = try values.decode(String.self, forKey: .email)
}
}
enum GHError: Error {
case invalidURL
case invalidResponse
case invalidData
}
Last Text("\(String(describing: jsonData?.developer))") parsing as below.
optional([App.Developer(email: "iosapps@gmail.com")])
I want to parse only mail ID from JSON array.
Please Let me know how can I achieve it. Thank in advance..