简体   繁体   中英

Parsing Nested JSON and storing into struct in Swift

{
  "0": {
    "productname": "Womens Air Jordan 1 Mid \u0027Canyon Rust\u0027 Shoes - Size 7.5W",
    "imageurl": "https://www.example.com/",
    "producturl": "https://www.example.com/",
    "price": "285.00",
    "currency": "USD",
    "saleprice": "",
    "storename": "Google"
  }
}

I need to store the product name inside of a struct but don't know how to access it within the '0'. This is my attempt below:

struct Shoes: Decodable {
    let 0: [ShoeItem];
}

struct ShoeItem: Decodable {
    let productname: String
}
{
  "0": {
    "productname": "Womens Air Jordan 1 Mid \u0027Canyon Rust\u0027 Shoes - Size 7.5W",
    "imageurl": "https://www.example.com/",
    "producturl": "https://www.example.com/",
    "price": "285.00",
    "currency": "USD",
    "saleprice": "",
    "storename": "Google"
  }
}

I need to store the product name inside of a struct but don't know how to access it within the '0'. This is my attempt below:

struct Shoes: Decodable {
    let 0: [ShoeItem];
}

struct ShoeItem: Decodable {
    let productname: String
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM