【问题标题】:ObjectMapper Nested(Triple) DictionariesObjectMapper 嵌套(三重)字典
【发布时间】:2019-03-08 06:52:09
【问题描述】:

下面的嵌套 Json 包

问题在于它将 json 字典对象转换为字符串,而不是正确的值。我不明白如何将 mvc 值作为 int 获取,将耗尽作为数组获取。

希望了解如何使用嵌套

{
        "id": 16,
        "user_id": 6,
        "name": 4,
        "med_gastro": "{'left': {'mvc': 0, 'effeciency_score': 0, 'exhaustion': [0, 0, 0]}, 'right': {'mvc': 0, 'effeciency_score': 0, 'exhaustion': [0, 0, 0]}}",
        "lat_gastro": "{'left': {'mvc': 0, 'effeciency_score': 0, 'exhaustion': [0, 0, 0]}, 'right': {'mvc': 0, 'effeciency_score': 0, 'exhaustion': [0, 0, 0]}}",
        "tib_anterior": "{'left': {'mvc': '13816.0', 'effeciency_score': 20.804231942965192, 'exhaustion': {'maxEffeciency': 10.16597510373444, 'subMaxEffeciency': 3.2009484291641965, 'minEffeciency': 86.63307646710136}, 'effeciency': 20.804231942965192}, 'right': {'mvc': '13816.0', 'effeciency_score': 0, 'exhaustion': [0, 0, 0]}}",
        "peroneals": "{'left': {'mvc': 0, 'effeciency_score': 0, 'exhaustion': [0, 0, 0]}, 'right': {'mvc': 0, 'effeciency_score': 0, 'exhaustion': [0, 0, 0]}}"
}

编码对象

import Foundation 
import ObjectMapper


class PlayerProfile : NSObject, NSCoding, Mappable{

    var id : Int?
    var latGastro : String?
    var medGastro : String?
    var name : Int?
    var peroneals : String?
    var tibAnterior : String?
    var userId : Int?


    class func newInstance(map: Map) -> Mappable?{
        return PlayerProfile()
    }
    required init?(map: Map){}
    private override init(){}

    func mapping(map: Map)
    {
        id <- map["id"]
        latGastro <- map["lat_gastro"]
        medGastro <- map["med_gastro"]
        name <- map["name"]
        peroneals <- map["peroneals"]
        tibAnterior <- map["tib_anterior"]
        userId <- map["user_id"]

    }

}

【问题讨论】:

  • 这不是因为它们在键周围包含单引号而不是双引号吗? "{'left': {'mvc': 0,... 不是正确的 json。你从哪里得到这个所谓的 json?无论如何,您可能需要手动处理这个问题,也许进行搜索并将单引号替换为双引号。

标签: swift xcode objectmapper


【解决方案1】:

使用可解码协议,您可以在修复字典字符串后手动解码它们。

据我所知,它们都有相同的结构,所以我们只需要为所有定义一组结构

struct DictionaryData: Codable {
    let itemLeft: Item
    let itemRight: Item?

    enum CodingKeys: String, CodingKey {
        case itemLeft = "left"
        case itemRight = "right"
    }
}

struct Item: Codable {
    let mvc, effeciencyScore: Int
    let exhaustion: [Int]

    enum CodingKeys: String, CodingKey {
        case mvc
        case effeciencyScore = "effeciency_score"
        case exhaustion
    }
}

首先我们需要修复字符串(假设我们有 PlayerProfile 对象,但这当然可以在类中完成)然后可以解码字符串。

let decoder = JSONDecoder()
if let medGastro = playerProfile.medGastro, let data = medGastro.data(using: .utf8) { 
    let fixedString = medGastro.replacingOccurrences(of: "'", with: "\"")
    do {
        let jsonDict = try decoder.decode(DictionaryData.self, from: data)
        // Do something with jsonDict 
    } catch {
        print(error)
    }
}

其他字段也一样,当然,因为这对于所有字段都是一样的,你可以把它放在类似的函数中

func parseString(_ string: String?) throws -> DictionaryData

【讨论】:

    猜你喜欢
    • 2016-12-25
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 2023-02-09
    • 2021-10-17
    • 2018-01-29
    相关资源
    最近更新 更多