How to make model class for following JSON response in swift iOS

前端 未结 6 1779
花落未央
花落未央 2021-02-10 14:40

Hi i am beginner for swift ios and my requirement is have to display Json response to table list i got response from web-services and response seems like below

MY requ

6条回答
  •  猫巷女王i
    2021-02-10 15:09

    Step1: Create your model like below.

    class Club { 
            var id: String = ""
            var name: String = ""
            var imageUrl: String = ""
            var hasVip: Bool = false
            var desc: String = ""
            var location = Location()
    
            init?(dictionary:[String:Any],location: Location) {
                guard let id = dictionary["_id"],
                    let name = dictionary["name"],
                    let imageUrl = dictionary["imageUrl"],
                    let hasVip = dictionary["hasVippler"]
                else {
                    return nil
                }
         self.id = id
         self.name = name
         self.imageUrl = imageUrl
         self.hasVip = hasVip
         self.location = location
      }
     }
    }
    class Location {
    
        var country: String = ""
        var city: String = ""
        var address: String = ""
        var zip: String = ""
    
       init?(dictionary:[String:Any]) {
                guard let country = dictionary["country"],
                    let city = dictionary["city"],
                    let address = dictionary["address"],
                    let zip = dictionary["zip"]
                else {
                    return nil
                }
         self.country = country
         self.city = city
         self.address = address
         self.zip = zip
      }
     }
    }
    

    Step2: Declare your array as below.

    var myTargetArray = [Club]()
    

    Step3: Json Parsing.

        do {
                if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSArray {
                    print("Response: \(json)")
    
                for club in json {
                       if let clubDictionary = club as? NSDictionary { 
                         if let locationDict = clubDictionary["location"] as? NSDictionary{
                             if let location = Location(dictionary: locationDict) {
    
                               if let clubObj = Club(dictionary: clubDictionary, location:location) {
                                myTargetArray.append(clubObj)
                           }
                       }
                    }
                }
            }
                } else {
                    let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)// No error thrown, but not NSDictionary
                    print("Error could not parse JSON: \(jsonStr)")
                }
            } catch let parseError {
                print(parseError)// Log the error thrown by `JSONObjectWithData`
                let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
                print("Error could not parse JSON: '\(jsonStr)'")
            }
    

    Note: I removed underground array from location model since your json data does not contain it. Finally you can use your target array as your tableview source. Happy coding...

提交回复
热议问题