简体   繁体   中英

How to model data from JSON array with no property name into swift so It can be parsed? SWIFT

Im stuck trying to model a JSON array which has no property name into my swift project with the goal of parsing the data and using it in my app. I know how to do this when there is a NAME for the array but I don't know how to make swift and this lackluster JSON understand each other. The path to the first "Company" in the JSON is "0.Company". The error I get is "Value of type 'WorkData' has no member '0'" Im including pictures of my full project so it is easier to understand the structure of the code and what im trying to do. Please look at the picture for a clearer understanding I apologize if Im not explaining it well i'm new to programming.

    import Foundation

class WorkData: Codable {
    let WorkData: [WorkData]
    let Company: String
    let worklogDate: String
    let issue: String
}
    func parseData(jsonDataInput: Data) {
        let decoder = JSONDecoder() // an object that decodes JSON data
        do {
            let decodedData = try decoder.decode(WorkData.self, from: jsonDataInput)
            let Company = decodedData.0.Company
            let worklogDate = decodedData.0.worklogDate
            let issue = decodedData.0.issue


        } catch {
            print (error)
        }
    }

}

json Trying to model JSON in Swift

Parsing JSON

You cannot start JSON with an array because JSON itself is an object {}

See example below:

{
    "WorkData" : [
                   {"Company" : ""},
                   {"Company" : ""},
                   {"Company" : ""}                                           
     ]

}

在此处输入图像描述

let decodedData = try decoder.decode(LocalizationsResponse.self, from: jsonDataInput)

decodedData will be an array

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