解析字典行为奇怪

Posted

技术标签:

【中文标题】解析字典行为奇怪【英文标题】:Parse dictionary behaviour strange 【发布时间】:2019-02-14 07:58:05 【问题描述】:

如果一个 key 值是 null 而另一个是键不为空,则解析字典值。

如果id 的值为null,则无法解析来自source 名称键的值。如果 id 的值不是null,则返回name 值。

    
    "status": "ok",
    "totalResults": 70,
    "articles": [
        
            "source": 
                "id": null,
                "name": "Bloombergquint.com"
            ,
            "author": "Aman Kapadia, Forum Bhatt",
            "title": "Infibeam Auditors Raise Questions On Loans To Vendors - BloombergQuint",
            "description": "Auditors to Infibeam Avenues raise questions about loans to vendors.",
            "url": "https://www.bloombergquint.com/business/infibeam-auditors-raise-questions-on-loans-to-vendors",
            "urlToImage": "https://images.assettype.com/bloombergquint%2F2018-02%2F11136839-3c0c-4356-b4d3-c918f4d417ad%2F323233043_1-9%20(1).jpg?rect=0%2C204%2C4000%2C2100&w=1200&auto=format%2Ccompress",
            "publishedAt": "2019-02-15T14:58:37Z",
            "content": "Auditors to Infibeam Avenues Ltd. raised questions about loans to vendors and how it recognises revenue in a fresh concern for the online retailer. SRBC & Co. LLP, the audit arm of EY, and Shah & Taparia sought more information to justify the rational… [+1985 chars]"
        ,
        
            "source": 
                "id": null,
                "name": "Moneycontrol.com"
            ,
            "author": null,
            "title": "RBI lifts cap on FPI investments in corporate bonds - Moneycontrol.com",
            "description": "While the provision was aimed at incentivising FPIs to maintain a portfolio of assets, market feedback indicates that foreign portfolio investors (FPIs) have been constrained by this stipulation, the RBI said.",
            "url": "https://www.moneycontrol.com/news/business/rbi-lifts-cap-on-fpi-investments-in-corporate-bonds-3545571.html",
            "urlToImage": "https://static-news.moneycontrol.com/static-mcnews/2017/03/RBI-770x433.jpg",
            "publishedAt": "2019-02-15T13:26:00Z",
            "content": "The Reserve Bank of India (RBI) Friday withdrew the 20 per cent limit on investments by FPIs in corporate bonds of an entity with a view to encourage more foreign investments. As part of the review of the FPI investment in corporate debt undertaken in April 2… [+696 chars]"
        ,
        
            "source": 
                "id": null,
                "name": "Rushlane.com"
            ,
            "author": "Rishabh Jain",
            "title": "2019 Honda Civic Review test drive - Still got the charm? - RushLane",
            "description": "Honda is back with the Civic in India. But does it have enough to attract those who love driving?",
            "url": "https://www.rushlane.com/2019-honda-civic-review-test-drive-12297473.html",
            "urlToImage": "https://www.rushlane.com/wp-content/uploads/2019/02/2019-honda-civic-review-test-drive-photos-specs-launch-price-14.jpg",
            "publishedAt": "2019-02-15T12:59:00Z",
            "content": "The Civic happens to be one of most loved brands from Honda in India. Globally too, it is one of those rare brands which have seen 10 proper generation changes. It is sold in multiple body types like Sedan, Coupe and Hatchback, however the Indian market only … [+8342 chars]"
        ,
        
            "source": 
                "id": null,
                "name": "Team-bhp.com"
            ,
            "author": "Aditya Nadkarni",
            "title": "Maruti Swift, Dzire, Ertiga could get CNG variants - Team-BHP",
            "description": "According to a media report, Maruti Suzuki might introduce CNG variants of its cars in the years to come. The move could be taken in response to comply with the BS-VI emission norms, which are scheduled ...",
            "url": "https://www.team-bhp.com/news/maruti-swift-dzire-ertiga-could-get-cng-variants",
            "urlToImage": "https://www.team-bhp.com/sites/default/files/styles/large/public/1_639.jpg",
            "publishedAt": "2019-02-15T12:43:30Z",
            "content": "According to a media report, Maruti Suzuki might introduce CNG variants of its cars in the years to come. The move could be taken in response to comply with the BS-VI emission norms, which are scheduled to be implemented in 2020 and tighter fuel efficiency no… [+1119 chars]"
        
   ]

模型类

private struct Headline: Decodable 
    let author: String
    let title: String
    let source: Source

    struct Source: Codable 
        var id : String
        var name : String
    

    private enum CodingKeys: String, CodingKey 

        case author
        case title
        case source
    

ViewController 内部

let url = ""
    let decoder = JSONDecoder()
    decoder.dateDecodingStrategy = .secondsSince1970
    // It is necessary for correct decoding. Timestamp -> Date.

        Alamofire.request(url, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil)
            .responseJSON  response in
                switch response.result 
                case .success:
//                    ??? how to assing here! access the value global????
                case .failure(let error):
                    print("Request failed with error: \(error)")
            
        
    

cellForRowAt 内部

cell.lbl_NewsTitle?.text = headlines[indexPath.row].title
cell.lbl_NewsSource?.text = headlines[indexPath.row].source["name"]

只有当 id 持有一些 String 值时才打印,否则 source.name 返回 nil

谁能告诉我为什么会这样? 我是第一次使用 swift 语言。 :(

【问题讨论】:

你的解析码呢?您只是显示一个 JSON,但没有代码... 如果 id 为空,您似乎没有创建源对象。 @DávidPásztor 让我编辑我的问题,以便它有足够的源代码来理解我所做的事情。 谁能告诉我如何提取响应数据以从源字典中提取名称值? 考虑放弃 ObjectMapper 以支持 Codable 协议。它是内置的,更高效,不需要 NSObject 子类。 【参考方案1】:

对于Decodable,您必须添加伞形结构,并且必须将Headline 中的authorSource 中的id 声明为可选,因为它们可以是null。所有的 CodingKey 都是合成的。

struct Response : Decodable 
    let status: String
    let totalResults: Int
    let articles: [Headline]


struct Headline: Decodable 
    let author: String?
    let title: String
    let source: Source
    let publishedAt : Date

    struct Source: Decodable 
        let id: String?
        let name: String
    
 

日期解码策略必须是.iso8601,而不是.secondsSince1970

此代码将articles 数组分配给数据源并在主线程上重新加载表视图

let url = ""

    Alamofire.request(url).responseJSON  response in
        switch response.result 
        case .success:
            let decoder = JSONDecoder()
            decoder.dateDecodingStrategy = .iso8601
            do 
               let result = try decoder.decode(Response.self, from: response.data!)
               self.headlines = result.articles
               DispatchQueue.main.async 
                    self.tableView.reloadData()
               
             catch  print("Decoding error:", error) 
        case .failure(let error):
            print("Request failed with error: \(error)")
        
    

数据源数组必须声明为

var headlines = [Headline]()

cellForRowAt

let headline = headlines[indexPath.row]
cell.lbl_NewsTitle?.text = headline.title
cell.lbl_NewsSource?.text = headline.source.name

【讨论】:

以上是关于解析字典行为奇怪的主要内容,如果未能解决你的问题,请参考以下文章

解析 JSON 字典时调试器中的不同类型

从 plist 文件中读取字典的奇怪行为

在 Swift 中使用 Alamofire 发送包含字典对象数组的字典对象时会发生奇怪的行为?

jQuery 选择器在从 ajax 解析的 HTML 中的行为不符合预期

字典操作中的奇怪行为以及 Python 中的多处理

奇怪的 PowerShell 递归行为