SwifityJSON 从其他 libraries/frameworks 解析 JSON 到 ViewController 的问题

SwifityJSON problem with parsing JSON to ViewController from other libraries/frameworks

我有以下 json 样子:

"message": "Something message here"
"data": [ 
  { "_id": "671672167bhw2hw"
    "photo": "https://uploads.example.com/671672167bhw2hw.jpg"
    "description": "Something description here"
    "date": "2020-07-01T09:44:15.448Z"
     "user": "John Doe"
  }
 ]
}

我使用 SwiftifyJSON 库来解码和解析 JSON。我的代码是这样的:

class Post: NSObject {
 var message: String
 var text: String?
 var photoURL: URL?
 var date: Date?
}

init(json: JSON) {
  self.user = User(id: json["user_id"].stringValue,
                         name: json["user"].stringValue,
                         photoURL: URL(string: json["user_photo"].stringValue))
  self.message = json["message"].stringValue
  self.objectId = json["data"]["_id"].stringValue
  self.text = json["data"]["description"].string
  self.date = DateFormatter.iso8601.date(from: json["data"]["date"].stringValue)
}

和我的 ApiManager 扩展

 static func getPosts(before date: Date? = nil, withSuccess success: @escaping (Bool, [Post]) -> Void) {
        guard let url = URL(string: ApiEndpoint.Wall.getPosts) else {
            success(false, [Post]())
            return
        }
        
        var json = [String: String]()
        
        DispatchQueue.global(qos: .background).async {
            ApiManager.request(url: url, method: .post, parameters: json) { response, statusCode in
                switch statusCode {
                case 200:
                    success(true, ApiManager.getWallPostsFrom(response: response))
                    return
                    
                default: break
                }
                
                success(false, [Post]())
                return
            }
        }
    }
    
    private static func getPostsFrom(response: DataResponse<Any>) -> [Post] {
        var result = [Post]()
        
        guard let value = response.result.value,
            let jsonArray = JSON(value).array else { return result }
        
        for json in jsonArray {
            let object = Post(json: json)
            result.append(object)
        }
        
        return result
    }
    
}

我收到 200 的响应,没关系。 但仍然无法在 PostViewController 中显示数据。怎么了?此外,我将自己的库用于联系人、消息、讨论面板等应用程序中的选项卡,因此我将自己的库与一些文件、模型、视图、任何选项卡的资产一起使用。

下面是我的 PostViewController,用于显示来自 JSON 的数据。

private func tryGetPosts() {
        canLoadPosts = false

        tableView.dataState = .downloading
         ApiManager.getPosts(before: Posts.last?.date) { [weak self] (success, objects) in
            self?.tableView.dataState = .noData
            
            print("Objects count: ", objects)

            guard objects.count > 0 else { return }

            #warning("Insert animation")
            self?.tableView.beginUpdates()
            self?.Posts += objects
            let indexPaths = (self!.Posts.count - objects.count ..< self!.Posts.count)
                .map { IndexPath(row: [=16=], section: 0) }
            self?.tableView.insertRows(at: indexPaths, with: .top)
            self?.tableView.endUpdates()
            self?.Posts.insert(contentsOf: objects, at: self?.Posts.count ?? 0)
            self?.tableView.reloadData()
            self?.canLoadWallPosts = true
        }

请多多指教。 最好的问候。

这个问题的答案在JSON(value).array

如果您的 JSON 结构有类似“数据”的数组,您可以写成 JSON(value)["data"].array。 SwiftyJSON 是一个非常友好和简单的库。