How to parse JSON using swift 4

ε祈祈猫儿з 提交于 2020-03-11 15:10:44

问题


I am confusing to getting detail of fruit

{
  "fruits": [
    {
      "id": "1",
      "image": "https://cdn1.medicalnewstoday.com/content/images/headlines/271/271157/bananas.jpg",
      "name": "Banana"
    },
    {
      "id": "2",
      "image": "http://soappotions.com/wp-content/uploads/2017/10/orange.jpg",
      "title": "Orange"
    }
  ]
}

Want to parse JSON using "Decodable"

struct Fruits: Decodable {
    let Fruits: [fruit]
}
struct fruit: Decodable {
    let id: Int?
    let image: String?
    let name: String?
}

let url = URL(string: "https://www.JSONData.com/fruits")
        URLSession.shared.dataTask(with: url!) { (data, response, error) in

            guard let data = data else { return }

            do{
                let fruits = try JSONDecoder().decode(Fruits.self, from: data)
                print(Fruits)

            }catch {
                print("Parse Error")
            }

also can you please suggest me cocoapod library for fastly download images


回答1:


The issue you are facing is because your JSON is returning different data for your Fruits.

For the 1st ID it returns a String called name, but in the 2nd it returns a String called title.

In addition when parsing the JSON the ID appears to be a String and not an Int.

Thus you have two optional values from your data.

As such your Decodable Structure should look something like this:

struct Response: Decodable {
    let fruits: [Fruits]

}

struct Fruits: Decodable {
    let id: String
    let image: String
    let name: String?
    let title: String?
}

Since your URL doesn't seem to be valid, I created the JSON file in my main bundle and was able to parse it correctly like so:

/// Parses The JSON
func parseJSON(){

    if let path = Bundle.main.path(forResource: "fruits", ofType: "json") {

        do {
            let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
            let jsonResult = try JSONDecoder().decode(Response.self, from: data)

            let fruitsArray = jsonResult.fruits

            for fruit in fruitsArray{

                print("""
                    ID = \(fruit.id)
                    Image = \(fruit.image)
                    """)

                if let validName = fruit.name{
                     print("Name = \(validName)")
                }

                if let validTitle = fruit.title{
                    print("Title = \(validTitle)")
                }


            }

        } catch {
           print(error)
        }
    }
}

Hope it helps...




回答2:


// Parse Json using decodable
// First in create Structure depends on json 
//
//
//

struct Countory : Decodable {
    let name: String
    let capital: String
    let region: String
}

 let url = "https://restcountries.eu/rest/v2/all"
 let urlObj = URL(string: url)!
 URLSession.shared.dataTask(with: urlObj) {(data, responds, Error) in

   do {
   var countories = try JSONDecoder().decode([Countory].self, from: data!)
    for country in countories {

       print("Country",country.name)
       print("###################")
       print("Capital",country.capital)

        }
        } catch {
         print(" not ")
        }
     }.resume()



回答3:


Model sample:

public struct JsonData: Codable{
    let data: [Data]?
    let meta: MetaValue?
    let linksData: LinksValue?

    private enum CodingKeys: String, CodingKey{
        case data
        case meta
        case linksData = "links"
    }
}

enum BackendError: Error {
    case urlError(reason: String)
    case objectSerialization(reason: String)
}

struct APIServiceRequest {

    static func serviceRequest<T>(reqURLString: String,
                                  resultStruct: T.Type,
                                  completionHandler:@escaping ((Any?, Error?) -> ())) where T : Decodable {
        guard let url = URL(string: reqURLString) else {
            print("Error: cannot create URL")
            let error = BackendError.urlError(reason: "Could not construct URL")
            completionHandler(nil, error)
            return
        }
        let urlRequest = URLRequest(url: url)

        let session = URLSession.shared

        let task = session.dataTask(with: urlRequest) { (data, response, error) in
            guard error == nil else {
                completionHandler(nil, error)
                return
            }

            guard let responseData = data else {
                print("Error: did not receive data")
                let error = BackendError.objectSerialization(reason: "No data in response")
                completionHandler(nil, error)
                return
            }

            let decoder = JSONDecoder()
            do {
                let books = try decoder.decode(resultStruct, from: responseData)
                completionHandler(books, nil)
            } catch {
                print("error trying to convert data to JSON")
                print(error)
                completionHandler(nil, error)
            }
        }

        task.resume()
    }
}

To Access:

let apiService = APIServiceRequest()

var dataArray: [String: Any]? //global var

apiService.serviceRequest(reqURLString: endPoint, resultStruct: VariantsModel.self, completionHandler: {dataArray,Error in})

POST Method

func loginWS(endpoint: String, completionHandler: @escaping (Any?) -> Swift.Void) {

    guard let sourceUrl = URL(string: endpoint) else { return }
    let request = NSMutableURLRequest(url: sourceUrl)

    let session = URLSession.shared
    request.httpMethod = "POST"
    request.addValue(vehiceHeader, forHTTPHeaderField: "X-Vehicle-Type")
    request.addValue(contentHeader, forHTTPHeaderField: "Content-Type")

    let task = session.dataTask(with: request as URLRequest) { data, response, error in

        guard let data = data else { return }
        do {
            let responseData = try JSONDecoder().decode(JsonData.self, from: data)
            print("response data:", responseData)
            completionHandler(responseData)
        } catch let err {
            print("Err", err)
        }
    }.resume()
}


来源:https://stackoverflow.com/questions/50379653/how-to-parse-json-using-swift-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!