如何将json字符串传递给对象?

时间:2019-07-05 21:45:29

标签: json swift dictionary object struct

我的问题是下一个,我有一个json

{
  "nombre" : "userProfile.user.name!",
  "apaterno" : 20,
  "amaterno" : true,
  "email" : 100,
  "start_screen" : {
    "info" : true,
    "title" : false,
    "image" : 20,
    "success_btn" : "hola",
    "close_btn" : true
  }
}

我想将此json传递给我的结构,我的结构是:

struct person: Decodable
{
    var email : Int
    var nombre : String
    var apaterno : Int
    var amaterno: Bool

    struct start_screen {
        var title: Bool
        var info: Bool
        var image: Int
        var success_btn: String
        var close_btn: Bool
    }
}

在接下来的几行中,我实现了将json放入我的结构中,但是start_screen结构无法获取数据。

let jsonData = json.data(using: .utf8)!
let decoder = JSONDecoder()
let myStruct = try! decoder.decode(person.self, from: jsonData)

当我访问myStruct.email时,我得到100,可以,但是我无法加载start_screen数据,该怎么办?

2 个答案:

答案 0 :(得分:1)

首先,您需要为const req = { headers: { origin: true }, }; person添加一个变量。

start_screen

然后,您需要制作var start_screen: start_screen start_screen

Decodable

这应该是使其生效的最少更改。


此外,您可能希望将类型大写。 struct start_screen: Decodable 的确让人困惑。您还可以设置变量并键入名称start_screen: start_screen,然后将camelCase转换为JSONDecoder。也是快速的命名约定。看起来像这样

snake_case

答案 1 :(得分:0)

这应该是您的 Person 结构:

struct Person: Decodable {
var email : Int?
var nombre : String?
var apaterno : Int?
var amaterno: Bool
var start_screen: Start_screen?

enum CodingKeys: String, CodingKey {

    case email = "email"
    case nombre = "nombre"
    case apaterno = "apaterno"
    case amaterno = "amaterno"
    case start_screen = "amaterno"
}

init(from decoder: Decoder) throws {
    let values = try decoder.container(keyedBy: CodingKeys.self)
    email = try values.decodeIfPresent(Int.self, forKey: .email)
    nombre = try values.decodeIfPresent(String.self, forKey: .nombre)
    apaterno = try values.decodeIfPresent(Int.self, forKey: .apaterno)
    amaterno = try values.decodeIfPresent(Bool.self, forKey: .apaterno) ?? false
    start_screen = try values.decodeIfPresent(Start_screen.self, forKey: .start_screen)
}

}

这应该是您的“开始屏幕” 结构:

struct Start_screen: Decodable {
var title: Bool
var info: Bool
var image: Int?
var success_btn: String?
var close_btn: Bool

enum CodingKeys: String, CodingKey {

    case title = "title"
    case info = "info"
    case image = "image"
    case success_btn = "success_btn"
    case close_btn = "close_btn"
}

init(from decoder: Decoder) throws {
    let values = try decoder.container(keyedBy: CodingKeys.self)
    title = try values.decodeIfPresent(Bool.self, forKey: .title) ?? false
    info = try values.decodeIfPresent(Bool.self, forKey: .info) ?? false
    image = try values.decodeIfPresent(Int.self, forKey: .image)
    success_btn = try values.decodeIfPresent(String.self, forKey: .success_btn)
    close_btn = try values.decodeIfPresent(Bool.self, forKey: .close_btn) ?? false
}

}

人员访问开始屏幕

if let jsonData = json.data(using: .utf8) {
   let user = try! JSONDecoder().decode(Person.self, from: jsonData)
   if let title = user.start_screen.title {
      print(title)
   }
}