JSON解码错误 - typeMissmatch Swift 4

时间:2018-04-09 19:26:38

标签: json swift swift4 codable decodable

我正在尝试从JSON解析一些数据,我已经使用了另一个API,但现在我有了另一个结构,我得到了typeMissmatch Erros ......

JSON看起来像:

{
"status": 200,
"data": {
"date": "2018-04-07T00:00:00.508Z",
"featured": [
  {
    "id": "2345",
    "name": "name",
    "price": "1,000",
    "priceIcon": "String",
    "priceIconLink": "URLString",
    "images": {
      "icon": "URLString",
      "png": "URLString",
      "gallery": "URLString",
      "featured": "URLString"
    },
    "rarity": "1String",
    "type": "1String",
    "readableType": "1String"
  }
],
"daily": [
        {
            "id": "12324",
            "name": "name",
            "price": "1,500",
            "priceIcon": "String",
            "priceIconLink": "URLString",
            "images": {
                "icon": "URLString",
                "png": "URLString",
                "gallery": "URLString",
                "featured": "URLString"
            },
            "rarity": "1String",
            "type": "1String",
            "readableType": "1String"
        }
    ]
}}

这样的Codable结构:

    struct Base : Codable {
    let status : Int
    let data : DataItems
   }

struct DataItems : Codable {
    let date : String
    let featured : [Featured]
    let daily : [Daily]
}

struct Featured : Codable {
    let id : String
    let name : String
    let price : String
    let priceIcon : String
    let priceIconLink : String
    let images : Images
    let rarity : String
    let type : String
    let readableType : String
}

struct Daily : Codable {
    let id : String
    let name : String
    let price : String
    let priceIcon : String
    let priceIconLink : String
    let images : Images
    let rarity : String
    let type : String
    let readableType : String
}

struct Images : Codable {
    let icon : String
    let png : String
    let gallery : String
    let featured : String

}

但是当我尝试解码那个Json时,我得到一个“Swift.DecodingError.typeMismatch”错误:

    ▿ Swift.DecodingError.typeMismatch
  ▿ typeMismatch: (2 elements)
    - .0: Swift.String #0
    ▿ .1: Swift.DecodingError.Context
      ▿ codingPath: 5 elements
        - CodingKeys(stringValue: "data", intValue: nil)
        - CodingKeys(stringValue: "daily", intValue: nil)
        ▿ _JSONKey(stringValue: "Index 0", intValue: 0)
          - stringValue: "Index 0"
          ▿ intValue: Optional(0)
            - some: 0
        - CodingKeys(stringValue: "images", intValue: nil)
        - CodingKeys(stringValue: "featured", intValue: nil)
      - debugDescription: "Expected to decode String but found a number instead."
      - underlyingError: nil

我的JSON解码器:

 enum Result<Value> {
    case success(Value)
    case failure(Error)
}

func getItems(for userId: Int, completion: ((Result<Base>) -> Void)?) {
    var urlComponents = URLComponents()
    urlComponents.scheme = "https"
    urlComponents.host = "api.jsonbin.io"
    urlComponents.path = "/myurl"
    let userIdItem = URLQueryItem(name: "userId", value: "\(userId)")
    urlComponents.queryItems = [userIdItem]
    guard let url = urlComponents.url else { fatalError("Could not create URL from components") }

    var request = URLRequest(url: url)
    request.httpMethod = "GET"


    let config = URLSessionConfiguration.default
    config.httpAdditionalHeaders = [
        "secret-key": "xyz"
    ]

    let session = URLSession(configuration: config)
    let task = session.dataTask(with: request) { (responseData, response, responseError) in
        DispatchQueue.main.async {
            if let error = responseError {
                completion?(.failure(error))
            } else if let jsonDataTest = responseData {
                // Now we have jsonData, Data representation of the JSON returned to us
                // from our URLRequest...

                // Create an instance of JSONDecoder to decode the JSON data to our
                // Codable struct
                let decoder = JSONDecoder()

                do {
                    // We would use Post.self for JSON representing a single Post
                    // object, and [Post].self for JSON representing an array of
                    // Post objects
                    let posts = try decoder.decode(Base.self, from: jsonDataTest)
                    completion?(.success(posts))
                } catch {
                    completion?(.failure(error))
                }
            } else {
                let error = NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey : "Data was not retrieved from request"]) as Error
                completion?(.failure(error))
            }
        }
    }

    task.resume()
}

var base:Base?

func loadJson() {
    getItems(for: 1) { (result) in
        switch result {
        case .success(let base):
            self.base = base
            dump(base)
        case .failure(let error):
          fatalError("error: \(error.localizedDescription)")
        }
    }
}

我是swift的新手,不知道这个错误告诉我什么或者问题“解码String但找到了一个数字”是什么。我认为我的结构有问题。我希望有人可以帮助我。

1 个答案:

答案 0 :(得分:3)

请显示要将数据解析为json的代码。

    let urlString = "your_url.json"
    guard let url = URL(string: urlString) else { return }

    URLSession.shared.dataTask(with: url) { (data, response, error) in
        if error != nil {
            print(error!.localizedDescription)
        }

        guard let data = data else { return }

            do {
            //Decode retrived data with JSONDecoder and assing type of Article object
            let baseData = try JSONDecoder().decode(Base.self, from: data)
            print(baseData) //whole project
            print(baseData.status) //200.0
            print(baseData.data.date)
            for day in baseData.data.daily {
                print(day.id)
                print(day.images.icon)
                print(day.images.featured)
                print(day.images.gallery)
                print(day.images.png)
                print(day.name)
                print(day.price)
                print(day.priceIcon)
                print(day.priceIconLink)
                print(day.rarity)
                print(day.readableType)
                print(day.type)
            }

            for feature in baseData.data.featured {
                print(feature.id)
                print(feature.images.icon)
                print(feature.images.featured)
                print(feature.images.gallery)
                print(feature.images.png)
                print(feature.name)
                print(feature.price)
                print(feature.priceIcon)
                print(feature.priceIconLink)
                print(feature.rarity)
                print(feature.readableType)
                print(feature.type)
            }
        } catch let jsonError {
            print(jsonError)
        }
        }.resume()

我试过这个,它对我有用。

顺便说一句,我有点困惑FeaturedDaily拥有所有相同的变量,但它们是不同的模型。

修改

您在问题中发布的数据有效。来自https://api.jsonbin.io/b/5acbd2dc214f9a2b84c6f167/1的json是错误的或不一致的。

"featured": false中有Daily,而Featured中有stringstring。在结构中是预期的APPS SCRIPT: function htmlOrders() { var active = SpreadsheetApp.getActive(); var sheet = active.getSheetByName("POHistory"); var lastRow = sheet.getLastRow(); var myRange = sheet.getRange("A2:A" + lastRow); var data = myRange.getValues(); var optionsHTML = ""; for (var i = 0; i < data.length; i+=1) { optionsHTML += '<td>' + data[i][0] + '</td>'; }; return optionsHTML; } HTML: <!DOCTYPE html> <html> <head> </head> <body> <table> <tr> <th>PO Number</th> <th>Vendor</th> <th>Date</th> <th>Total</th> </tr> <td> < ?!= htmlOrders(); ?> </td> </table> </body> </html> 。所以你会得到不匹配。一旦你尝试解析一个字符串(工作),然后你尝试将一个布尔值解析为一个字符串(错误)。