使用swift返回JSON数组中的特定对象



使用结构体,我通过web API从JSON获取数据,我想打印特定对象的所有数据,例如,我想从响应中打印所有产品Id。

下面是我的代码:
struct Varients: Decodable {
let ProductId: String
let Colour: String
let name: String
}
struct varientsResponse: Decodable {
let varients: [Varients]
}

视图控制器:

let url = URL(string: "http://192.168.1.113:8000/getvarient/?url=https://www.prettylittlething.com/stone-abstract-marble-print-structured-corset.html")
guard let requestUrl = url else { fatalError() }
// Create URL Request
var request = URLRequest(url: requestUrl)
// Specify HTTP Method to use
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in

// Check if Error took place
if let error = error {
print("Error took place (error)")
return
}

// Read HTTP Response Status code
if let response = response as? HTTPURLResponse {
print("Response HTTP Status code: (response.statusCode)")
}

// Convert HTTP Response Data to a simple String
if let data = data, let dataString = String(data: data, encoding: .utf8) {
let varientResponse = try? JSONDecoder().decode(varientsResponse.self, from: data)
print(varientResponse)



}

}
task.resume()

JSON响应示例:

"varients": [
{
"ProductId": "1703412",
"Colour": "Stone",
"Sizes": [
"4",
"6",
"8",
"10",
"12",
"14",
"16"
],
"image": "https://cdn-img.prettylittlething.com/2/7/f/4/27f4825ee181668f1e8e5797478607b505dbee1c_cmt4421_1.jpg?imwidth=1024",
"name": "Stone  Abstract Marble Print Structured Corset"
},
{
"ProductId": "865801",
"Colour": "Pastel Orange",
"Sizes": [
"4",
"6",
"8",
"10",
"12",
"14",
"16"
],
"image": "https://cdn-img.prettylittlething.com/e/b/e/6/ebe60bba9afe0fc20fabbb5db34494dcd688dc4b_CMF1440_1.jpg?imwidth=1024",
"name": "Orange Tie Dye Print Structured Corset Top"
},
{
"ProductId": "867743",
"Colour": "Purple",
"Sizes": [
"4",
"6",
"8",
"10",
"12",
"14",
"16"
],
"image": "https://cdn-img.prettylittlething.com/d/a/8/3/da83149d0c6e00e094e417f3ed2d4b15927d15a9_CMF1829_1.jpg?imwidth=1024",
"name": "Purple Tie Dye Print Structured Corset Top"
},
{
"ProductId": "979950",
"Colour": "Fuchsia",
"Sizes": [
"4",
"6",
"8",
"10",
"12",
"14",
"16"
],
"image": "https://cdn-img.prettylittlething.com/a/0/e/2/a0e2d0847f64ee961388272009203461ee7028ee_cmg7868_1.jpg?imwidth=1024",
"name": "Fuschia Tie Dye Print Structured Corset Top"
},
{
"ProductId": "1083745",
"Colour": "Acid Blue Wash",
"Sizes": [
"4",
"6",
"8",
"10",
"12",
"14",
"16"
],
"image": "https://cdn-img.prettylittlething.com/b/e/7/4/be744c7f8fef860182ea730572c3f1dfb8b4820e_cmk5407_1.jpg?imwidth=1024",
"name": "Blue Tie Dye Print Structured Corset Top"
},
{
"ProductId": "1083753",
"Colour": "Light Pink",
"Sizes": [
"4",
"6",
"8",
"10",
"12",
"14",
"16"
],
"image": "https://cdn-img.prettylittlething.com/f/c/8/7/fc8700bbd28684d418f4ed49b638bda9112b1aca_cmk5405_1.jpg?imwidth=1024",
"name": "Light Pink Tie Dye Print Structured Corset Top"
},
{
"ProductId": "1450163",
"Colour": "Turquoise",
"Sizes": [
"4",
"6",
"8",
"10",
"12",
"14",
"16"
],
"image": "https://cdn-img.prettylittlething.com/6/6/6/f/666fc73c32602aa964f69d65fee2e3f77198448d_cmp4910_1.jpg?imwidth=1024",
"name": "Turquoise Marble Print Structured Corset Top"
},
{
"ProductId": "1667090",
"Colour": "Pink",
"Sizes": [
"4",
"6",
"8",
"10",
"12",
"14",
"16"
],
"image": "https://cdn-img.prettylittlething.com/f/8/a/8/f8a85d97fe670e37ec5903d435e1ac44ecad8fd7_cms8800_1.jpg?imwidth=1024",
"name": "Pink Abstract Renaissance Print Structured Corset"
},
{
"ProductId": "1698866",
"Colour": "Monochrome",
"Sizes": [
"4",
"6",
"8",
"10",
"12",
"14",
"16"
],
"image": "https://cdn-img.prettylittlething.com/f/7/a/a/f7aa8247197d6dcfc8f8bd270d7b50626e857d62_cmt3674_1.jpg?imwidth=1024",
"name": "Black Zebra Print Structured Corset Top"
},
{
"ProductId": "1703508",
"Colour": "Green",
"Sizes": [
"4",
"6",
"8",
"10",
"12",
"14",
"16"
],
"image": "https://cdn-img.prettylittlething.com/c/b/d/f/cbdf70eaa970a1ffaa3f3b54e9b7c4173ee0c560_cmt4422_1.jpg?imwidth=1024",
"name": "Green  Abstract Marble Print Structured Corset"
}
]
}

我可以返回varientsResponse,但我显然得到完整的JSON响应,我也可以返回varientsResponse.varients[0]。但我希望能够得到JSON中所有productId的完整数组

回答你最初的问题,然后在评论中回答你的后续问题。

您可以像这样访问特定项目的颜色和ID:

varientResponse.variants.map { varient in 

// Save to database by accessing like this:
// Colour - varient.Colour
// ID - varient.ProductID
}

与你的问题无关,但通常struct被命名为单数。这是有意义的,因为你的结构的每个实例将是一个单一的Varient

最新更新