首页 > 解决方案 > 解析 JSON 数据以填充 TableView

问题描述

我有一个 JSON(目前在本地),我想解析它以将这些数据放在 listView 中。

我已经创建了视图并尝试了一些方法(例如本教程:https ://www.journaldev.com/21839/ios-swift-json-parsing-tutorial )来解析该 JSON,但没有成功。

这是我尝试过的一些代码:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var labelHeader: UILabel!
@IBOutlet weak var tableView: UITableView!

var channelList = [channelData]()

override func viewDidLoad() {
    super.viewDidLoad()
let url = Bundle.main.url(forResource: "channels", withExtension: "json")

    guard let jsonData = url
        else{
            print("data not found")
            return
    }

    guard let data = try? Data(contentsOf: jsonData) else { return }

    guard let json = try? JSONSerialization.jsonObject(with: data, options: []) else{return}

    if let dictionary = json as? [String: Any] {

        if let title = dictionary["title"] as? String {
            print("in title")
            labelHeader.text = title
        }

        if let data = dictionary["data"] as? Any {
            print("data is \(data)")
        }
        if let date = dictionary["date"] as? Date {
            print("date is \(date)")
        }
        // And so on

        for (key, value) in dictionary {
            print("Key is: \(key) and value is \(value)" )
            //This print the whole JSON to the console.
        }
    }

    //Now lets populate our TableView
    let newUrl = Bundle.main.url(forResource: "channels", withExtension: "json")

    guard let j = newUrl
        else{
            print("data not found")
            return
    }

    guard let d = try? Data(contentsOf: j)
        else { print("failed")
            return
    }

    guard let rootJSON = try? JSONSerialization.jsonObject(with: d, options: [])
        else{ print("failedh")
            return
    }

    if let JSON = rootJSON as? [String: Any] {
        labelHeader.text = JSON["id"] as? String //Should update the Label in the ListView with the ID found in the JSON

        guard let jsonArray = JSON["type"] as? [[String: Any]] else {
            return
        }

        let name = jsonArray[0]["name"] as? String
        print(name ?? "NA")
        print(jsonArray.last!["date"] as? Int ?? 1970)

        channelList = jsonArray.compactMap{return channelData($0)}

        self.tableView.reloadData()

    }
}

这是 JSON 文件的示例:

{
"format": "json",
"data": [
    {
        "type": "channel",
        "id": "123",
        "updated_at": "2019-05-03 11:32:57",
        "context": "search",
        "relationships": {
            "recipients": [
                {
                    "type": "user",
                    "id": 321,
                    "participant_id": 456
                }
            ],
            "search": {
                "type": "search",
                "title": "Title"
            },
        }
    },

我想找到使用这种 JSON 的最佳方式。

目前我无法将数据获取到 listView。我拥有的最多的是 xCode 控制台中的 JSON(至少这意味着我能够打开 JSON)。

标签: jsonswiftuitableviewparsing

解决方案


在 Swift 4+ 中解析 JSON 的推荐方法是Codable协议。

创建结构

struct Root: Decodable {
    let format: String
    let data: [ChannelData]
}

struct ChannelData: Decodable {
    let type, id, updatedAt, context: String
    let relationships: Relationships
}

struct Relationships: Decodable {
    let recipients: [Recipient]
    let search: Search
}

struct Recipient: Decodable {
    let type: String
    let id: Int
    let participantId: Int
}

struct Search: Decodable {
    let type: String
    let title: String
}

由于该channels.json文件位于应用程序包中并且无法修改,因此您可以减少viewDidLoad

var channelList = [ChannelData]()

override func viewDidLoad() {
    super.viewDidLoad()
    let url = Bundle.main.url(forResource: "channels", withExtension: "json")!
    let data = try! Data(contentsOf: url)
    let decoder = JSONDecoder()
    decoder.keyDecodingStrategy = .convertFromSnakeCase
    let result = try! decoder.decode(Root.self, from: data)
    channelList = result.data
    self.tableView.reloadData()
}

如果代码崩溃,则表明存在设计错误。结构与问题中的 JSON 匹配。可能它要大得多,那么您必须调整或扩展结构。


推荐阅读