代码之家  ›  专栏  ›  技术社区  ›  kidl33t

将麻烦的JSON字符串转换为Swift中的对象

  •  -1
  • kidl33t  · 技术社区  · 6 年前

    我认为数据包含换行符(“\r\n”)这一事实可能有问题,因此我也尝试删除这些字符,以及删除空白(有很多,我想做一些讨厌的和不可打印的东西,但不在其中)。绳子在这里,很长。

    var rawData: String = "{\r\n    \"contacts\": [\r\n        {\r\n                \"id\": \"c200\",\r\n                \"name\": \"Ravi Tamada\",\r\n                \"email\": \"ravi@gmail.com\",\r\n                \"address\": \"xx-xx-xxxx,x - street, x - country\",\r\n                \"gender\" : \"male\",\r\n                \"phone\": {\r\n                    \"mobile\": \"+91 0000000000\",\r\n                    \"home\": \"00 000000\",\r\n                    \"office\": \"00 000000\"\r\n                }\r\n        },\r\n        {\r\n                \"id\": \"c201\",\r\n                \"name\": \"Johnny Depp\",\r\n                \"email\": \"johnny_depp@gmail.com\",\r\n                \"address\": \"xx-xx-xxxx,x - street, x - country\",\r\n                \"gender\" : \"male\",\r\n                \"phone\": {\r\n                    \"mobile\": \"+91 0000000000\",\r\n                    \"home\": \"00 000000\",\r\n                    \"office\": \"00 000000\"\r\n                }\r\n        },\r\n        {\r\n                \"id\": \"c202\",\r\n                \"name\": \"Leonardo Dicaprio\",\r\n                \"email\": \"leonardo_dicaprio@gmail.com\",\r\n                \"address\": \"xx-xx-xxxx,x - street, x - country\",\r\n                \"gender\" : \"male\",\r\n                \"phone\": {\r\n                    \"mobile\": \"+91 0000000000\",\r\n                    \"home\": \"00 000000\",\r\n                    \"office\": \"00 000000\"\r\n                }\r\n        },\r\n        {\r\n                \"id\": \"c203\",\r\n                \"name\": \"John Wayne\",\r\n                \"email\": \"john_wayne@gmail.com\",\r\n                \"address\": \"xx-xx-xxxx,x - street, x - country\",\r\n                \"gender\" : \"male\",\r\n                \"phone\": {\r\n                    \"mobile\": \"+91 0000000000\",\r\n                    \"home\": \"00 000000\",\r\n                    \"office\": \"00 000000\"\r\n                }\r\n        },\r\n        {\r\n                \"id\": \"c204\",\r\n                \"name\": \"Angelina Jolie\",\r\n                \"email\": \"angelina_jolie@gmail.com\",\r\n                \"address\": \"xx-xx-xxxx,x - street, x - country\",\r\n                \"gender\" : \"female\",\r\n                \"phone\": {\r\n                    \"mobile\": \"+91 0000000000\",\r\n                    \"home\": \"00 000000\",\r\n                    \"office\": \"00 000000\"\r\n                }\r\n        },\r\n        {\r\n                \"id\": \"c205\",\r\n                \"name\": \"Dido\",\r\n                \"email\": \"dido@gmail.com\",\r\n                \"address\": \"xx-xx-xxxx,x - street, x - country\",\r\n                \"gender\" : \"female\",\r\n                \"phone\": {\r\n                    \"mobile\": \"+91 0000000000\",\r\n                    \"home\": \"00 000000\",\r\n                    \"office\": \"00 000000\"\r\n                }\r\n        },\r\n        {\r\n                \"id\": \"c206\",\r\n                \"name\": \"Adele\",\r\n                \"email\": \"adele@gmail.com\",\r\n                \"address\": \"xx-xx-xxxx,x - street, x - country\",\r\n                \"gender\" : \"female\",\r\n                \"phone\": {\r\n                    \"mobile\": \"+91 0000000000\",\r\n                    \"home\": \"00 000000\",\r\n                    \"office\": \"00 000000\"\r\n                }\r\n        },\r\n        {\r\n                \"id\": \"c207\",\r\n                \"name\": \"Hugh Jackman\",\r\n                \"email\": \"hugh_jackman@gmail.com\",\r\n                \"address\": \"xx-xx-xxxx,x - street, x - country\",\r\n                \"gender\" : \"male\",\r\n                \"phone\": {\r\n                    \"mobile\": \"+91 0000000000\",\r\n                    \"home\": \"00 000000\",\r\n                    \"office\": \"00 000000\"\r\n                }\r\n        },\r\n        {\r\n                \"id\": \"c208\",\r\n                \"name\": \"Will Smith\",\r\n                \"email\": \"will_smith@gmail.com\",\r\n                \"address\": \"xx-xx-xxxx,x - street, x - country\",\r\n                \"gender\" : \"male\",\r\n                \"phone\": {\r\n                    \"mobile\": \"+91 0000000000\",\r\n                    \"home\": \"00 000000\",\r\n                    \"office\": \"00 000000\"\r\n                }\r\n        },\r\n        {\r\n                \"id\": \"c209\",\r\n                \"name\": \"Clint Eastwood\",\r\n                \"email\": \"clint_eastwood@gmail.com\",\r\n                \"address\": \"xx-xx-xxxx,x - street, x - country\",\r\n                \"gender\" : \"male\",\r\n                \"phone\": {\r\n                    \"mobile\": \"+91 0000000000\",\r\n                    \"home\": \"00 000000\",\r\n                    \"office\": \"00 000000\"\r\n                }\r\n        },\r\n        {\r\n                \"id\": \"c2010\",\r\n                \"name\": \"Barack Obama\",\r\n                \"email\": \"barack_obama@gmail.com\",\r\n                \"address\": \"xx-xx-xxxx,x - street, x - country\",\r\n                \"gender\" : \"male\",\r\n                \"phone\": {\r\n                    \"mobile\": \"+91 0000000000\",\r\n                    \"home\": \"00 000000\",\r\n                    \"office\": \"00 000000\"\r\n                }\r\n        },\r\n        {\r\n                \"id\": \"c2011\",\r\n                \"name\": \"Kate Winslet\",\r\n                \"email\": \"kate_winslet@gmail.com\",\r\n                \"address\": \"xx-xx-xxxx,x - street, x - country\",\r\n                \"gender\" : \"female\",\r\n                \"phone\": {\r\n                    \"mobile\": \"+91 0000000000\",\r\n                    \"home\": \"00 000000\",\r\n                    \"office\": \"00 000000\"\r\n                }\r\n        },\r\n        {\r\n                \"id\": \"c2012\",\r\n                \"name\": \"Eminem\",\r\n                \"email\": \"eminem@gmail.com\",\r\n                \"address\": \"xx-xx-xxxx,x - street, x - country\",\r\n                \"gender\" : \"male\",\r\n                \"phone\": {\r\n                    \"mobile\": \"+91 0000000000\",\r\n                    \"home\": \"00 000000\",\r\n                    \"office\": \"00 000000\"\r\n                }\r\n        }\r\n    ]\r\n}"
    

    here

    任何想法,想法,或工作样本将是伟大的。我知道这是一个听起来很常见的问题,但我几乎试过了所有的方法,还是没能奏效。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Sweeper    6 年前

    JSONDecoder 不关心空格或换行符,只要它是格式良好的JSON。想想你的模型应该是什么样子,而不是担心换行符。

    通过快速查看JSON,我创建了以下模型结构:

    struct Phone : Codable {
        let mobile: String
        let home: String
        let office: String
    }
    
    struct Contact : Codable {
        let id: String
        let name: String
        let email: String
        let address: String
        let gender: String
        let phone: Phone
    }
    
    struct Contacts : Codable {
        let contacts: [Contact]
    }
    

    你只需要解码一个 Contacts

    let json = try! Data.init(contentsOf: URL(string: "https://api.androidhive.info/contacts/")!)
    let decoder = JSONDecoder()
    let contacts = try! decoder.decode(Contacts.self, from: json)
    

    Data.init(contentsOf:) 获取数据。这比自己复制和粘贴字符串要可靠得多。

    另请注意,Swift中有多行字符串文字:

    let jsonString = """
    your multiline JSON can go here...
    """