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

时间:2018-09-12 15:04:02

标签: json swift string object

我有一个无法转换为JSON的字符串。我已经尝试了JSONDecode,JSONSerialization以及搜索的前几页中建议的其他所有内容。我还尝试将其解码为可编码的结构。

我认为数据可能包含换行符(“ \ 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 个答案:

答案 0 :(得分:1)

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)

无法解码的一个可能原因可能是您在复制JSON字符串并将其变成一行时出错。如您所见,我使用Data.init(contentsOf:)来获取数据。这比您自己复制并粘贴字符串要可靠得多。

还要注意,Swift中有多行字符串文字:

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