我有一个GEOSWIFT功能,如下所示:

{
  "type" : "Feature",
  "geometry" : {
    "type" : "Point",
    "coordinates" : [
      -xx.xxxxxxxxxxxxxxx,
      xx.xxxxxxxxxxxxxxx
    ]
  },
  "properties" : {
    "mapLayer" : "MyMapLayer",
    "data" : {
      "id" : 42,
      "sizeClass" : "Large",
    // and so on...
    },
    "featureType" : "MyFeatureType"
  }
}

我希望检索data个成员,并将其放入匹配的 struct 中:

struct MyStruct: Decodable {
    var id: Int
    var sizeClass: String?
    // and so on...
}

这段代码只会让我得到data,但数据类型是GEOSwift.JSON,我不知道如何将其字符串化为使用常见的JSONDecoder类进行解码.

if case let .object(data) = feature.properties?["data"] {
  // do stuff with data: GEOSwift.JSON to get it into MyStruct
}

下面是GEOSwift.JSON枚举:

import Foundation

public enum JSON: Hashable, Sendable {
    case string(String)
    case number(Double)
    case boolean(Bool)
    case array([JSON])
    case object([String: JSON])
    case null

    /// Recursively unwraps and returns the associated value
    public var untypedValue: Any {
        switch self {
        case let .string(string):
            return string
        case let .number(number):
            return number
        case let .boolean(boolean):
            return boolean
        case let .array(array):
            return array.map { $0.untypedValue }
        case let .object(object):
            return object.mapValues { $0.untypedValue }
        case .null:
            return NSNull()
        }
    }
}

extension JSON: ExpressibleByStringLiteral {
    public init(stringLiteral value: String) {
        self = .string(value)
    }
}

extension JSON: ExpressibleByIntegerLiteral {
    public init(integerLiteral value: Int) {
        self = .number(Double(value))
    }
}

extension JSON: ExpressibleByFloatLiteral {
    public init(floatLiteral value: Double) {
        self = .number(value)
    }
}

extension JSON: ExpressibleByBooleanLiteral {
    public init(booleanLiteral value: Bool) {
        self = .boolean(value)
    }
}

extension JSON: ExpressibleByArrayLiteral {
    public init(arrayLiteral elements: JSON...) {
        self = .array(elements)
    }
}

extension JSON: ExpressibleByDictionaryLiteral {
    public init(dictionaryLiteral elements: (String, JSON)...) {
        let object = elements.reduce(into: [:]) { (result, element) in
            result[element.0] = element.1
        }
        self = .object(object)
    }
}

extension JSON: ExpressibleByNilLiteral {
    public init(nilLiteral: ()) {
        self = .null
    }
}

推荐答案

您必须将Feature.Properties["data"]重新编码为JSON,然后将其解码为MyStruct.您不能直接这样做,因为GEOSwift.JSON不符合Decodable,但它确实符合Encoable,所以您对其进行编码,然后再对其进行解码.

let myStructData = feature.properties?["data"] ?? nil
let jsonData = try! JSONEncoder().encode(myStructData)
let decoder = JSONDecoder()
myStruct = try decoder.decode(MyStruct.self, from: jsonData)

Json相关问答推荐

从Razor Pages的AJAX Json呈现DataTables问题.Net GET

使用JQ将JSON输出转换为CSV复杂 struct

Ansible - 将文件内容添加到字典中

将JSON行转换为TSV格式, for each 数组项生成单独的行

如何在 Apache NiFi 中使用 JoltTransformJson 删除流文件或具有空字段的整个对象?

jq :遍历 json 并在键存在时检索集合

如何 Select 一个值,这是可选的 - 使用 jq

如何将 XML 转换为 PsCustomObject 以允许最终导出为 JSON?

如何在生产环境中更改 Flutter 应用程序中的数据模型?

如何判断字符串是否为json格式

将字符串映射到json对象的多种类型?

jQuery fullcalendar 发送自定义参数并使用 JSON 刷新日历

在 JSON.NET 中序列化派生类时的字段顺序

春天:返回@ResponseBodyResponseEntity>

类型是接口或抽象类,不能实例化

使用 JSON.NET 序列化/反序列化对象字典

Backbone.js 模型与集合

Javascript对象和JSON对象有什么区别

使用 JavaScriptSerializer() 反序列化 JSON 文件

MVC ajax json 发布到控制器操作方法