【问题标题】:SwiftyJSON convert string array to array not workingSwiftyJSON将字符串数组转换为数组不起作用
【发布时间】:2020-10-13 11:12:45
【问题描述】:

我的数据如下:

data = 
[
"\u65b0\u5317\u5e02\u4e09\u91cd\u5340","\u65b0\u5317\u5e02\u6c38\u548c\u5340",
"\u53f0\u5317\u5e02\u4e2d\u5c71\u5340","\u53f0\u5317\u5e02\u4e2d\u6b63\u5340",
"\u53f0\u5317\u5e02\u4fe1\u7fa9\u5340","\u53f0\u5317\u5e02\u5357\u6e2f\u5340",
"\u53f0\u5317\u5e02\u5927\u540c\u5340","\u53f0\u5317\u5e02\u5927\u5b89\u5340",
"\u53f0\u5317\u5e02\u6587\u5c71\u5340","\u53f0\u5317\u5e02\u677e\u5c71\u5340",
"\u53f0\u5317\u5e02\u842c\u83ef\u5340"
]

但是当我想将其转换为数组时,我使用代码:

data.array

它总是给我零,我该怎么办?

我也试过data.arrayValuedata.arrayValue.map {$0.stringValue}

【问题讨论】:

  • 不确定这里是否相关,因为我不使用 SwiftyJSON,但 swift 字符串中十六进制值的正确格式是 \u{65b0}
  • 您的data 类型是什么?
  • @OlegBaidalka 我的数据类型实际上只是字符串

标签: json swift swifty-json


【解决方案1】:

假设你的数据结构是

[
    {
        "data": [
            "\u65b0\u5317\u5e02\u4e09\u91cd\u5340",
            "\u65b0\u5317\u5e02\u6c38\u548c\u5340",
            "\u53f0\u5317\u5e02\u4e2d\u5c71\u5340",
            "\u53f0\u5317\u5e02\u4e2d\u6b63\u5340",
            "\u53f0\u5317\u5e02\u4fe1\u7fa9\u5340",
            "\u53f0\u5317\u5e02\u5357\u6e2f\u5340",
            "\u53f0\u5317\u5e02\u5927\u540c\u5340",
            "\u53f0\u5317\u5e02\u5927\u5b89\u5340",
            "\u53f0\u5317\u5e02\u6587\u5c71\u5340",
            "\u53f0\u5317\u5e02\u677e\u5c71\u5340",
            "\u53f0\u5317\u5e02\u842c\u83ef\u5340"
        ]
    }
]

将JSON转化为实体同时符合Codable Protocol

typealias DistEntity = [Dist]

struct Dist: Codable {
    let data: [String]
}

实现模型层

protocol JSONFetcher: AnyObject {
    func distParser(forResource fileName: String, completionHandler handler: @escaping((Result<DistEntity, Error>) -> ()))
}

class ModelLayer: JSONFetcher {

    enum ParserError: Error {
        case PathNotFound
        case ConvertsObjectError
        case DecoderError
    }

    func distParser(forResource fileName: String, completionHandler handler: @escaping((Result<DistEntity, Error>) -> ())) {
        guard let url = Bundle.main.url(forResource: fileName, withExtension: "json") else { return handler(.failure(ParserError.PathNotFound)) }
        guard let jsonData = try? Data(contentsOf: url) else { return handler(.failure(ParserError.ConvertsObjectError)) }
        let decoder = JSONDecoder()
        decoder.dateDecodingStrategy = .iso8601
        guard let distEntity: DistEntity = try? decoder.decode(DistEntity.self, from: jsonData) else { return handler(.failure(ParserError.DecoderError)) }
        handler(.success(distEntity))
    }
}

业务逻辑层接口中的初步解析

final class viewModel {

    private var fetcher: JSONFetcher

    init(fetcher: JSONFetcher = ModelLayer()) {
        self.fetcher = fetcher
    }

    private func distParser() {
        self.fetcher.distParser(forResource: "YourJSONFileName") { (result) in
            switch result {
            case .success(let entity):
                print("[Dist Entity]: \(entity)")
            case .failure(let error):
                print(error.localizedDescription)
            }
        }
    }
}

无法确保这对您的场景有用,

如果没有解决,能否提供更详细的信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    相关资源
    最近更新 更多