如何使用Gloss访问嵌套的JSON数组值?

时间:2017-01-30 01:49:40

标签: arrays json swift

我目前正在使用图库Gloss:https://github.com/hkellaway/Gloss

以帮助解析JSON数据。我似乎无法访问嵌套数组,如“know_for”值:

    "results": [
        {
          "profile_path": "/s2eX7lG0n35AtkenfZV0uoLSudv.jpg",
          "adult": false,
          "id": 77700,
          "known_for": [
            {
              "poster_path": "/ckrTPz6FZ35L5ybjqvkLWzzSLO7.jpg",
              "adult": false,
              "overview": "",
              "release_date": "2016-05-25",
              "original_title": "Warcraft",
              "genre_ids": [
                12,
                14,
                28
              ]
       }
    ]
  }
]

这就是我设置数据对象的方法:

    public struct ResultsPeopleData: Decodable {

      public let results: [PeopleData]?

      public init?(json: JSON) {

        results = "results" <~~ json
      }
    }


    public struct knownForArray : Decodable {

      public let poster : String
      public let overview : String
      public let releaseDate : String
      public let title : String
      public let id : NSNumber
      public let backdrop : String

      public init?(json: JSON) {

        guard let poster : String = "poster_path" <~~ json,
          let overview : String = "overview" <~~ json ?? "N/A",
          let releaseDate : String = "release_date" <~~ json ?? "N/A",
          let title : String = "title" <~~ json ?? "N/A",
          let id : NSNumber = "id" <~~ json,
          let backdrop : String = "backdrop_path" <~~ json
          else { return nil }

        self.poster = poster
        self.overview = overview
        self.releaseDate = releaseDate
        self.title = title
        self.id = id
        self.backdrop = backdrop
      }
    }

    public struct PeopleData: Decodable {

      public let profile : String
      public let id : NSNumber
      public let knownFor : [knownForArray]?
      public let name : String

      public init? (json: JSON) {

        guard let profile : String = "profile_path" <~~ json ?? "N/A",
          let id : NSNumber = "id" <~~ json,
          let knownFor : knownForArray = "known_for" <~~ json,// This causes function to return
          let name : String = "name" <~~ json
          else { return nil }

        self.profile = profile
        self.id = id
        self.knownFor = [knownFor]
        self.name = name
      }

这是我更新数据对象的方式:

  static func updateAllData(urlExtension: String, completionHandler:@escaping (_ details: [PeopleData]?) -> Void){

    let nm = NetworkManager.sharedManager

    nm.getJSONData(type:"person", urlExtension: urlExtension, completion: {
      data in

      if let jsonDictionary = nm.parseJSONData(data)
      {
        guard let peopleResults = ResultsPeopleData(json: jsonDictionary)

          else {
            print("Error initializing object")
            return
        }

        print(jsonDictionary)

        guard let peopleData = peopleResults.results
          else {
            print("No such item") //This gets triggered when updating "knownfor" value
            return
        }

        completionHandler(peopleData)
      }
    })
  }
}

我在对象值的inits中放置了断点,每次我尝试初始化PeopleData的`

  

public let knownFor:[knownForArray]?

`值它停止初始化PeopleData结构并导致

  

打印(“没有这样的项目”)

要触发的updateAllData函数的一部分。我不确定我做错了什么。

0 个答案:

没有答案