Gson equivalent in Objective-C

后端 未结 8 1467
我寻月下人不归
我寻月下人不归 2021-02-05 09:14

Is there any equivalent to gson in Objective-C?

Thanks.

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-05 10:01

    At WWDC 2017, Apple has introduced the new feature in Swift to parse JSON without any pain using Swift Codable protocol

    struct YourStructure: Codable {
        let name: String?
        let avatarUrl: URL?
        private enum CodingKeys: String, CodingKey {
            case name
            case avatarUrl = "avatar_url"
        }
    }
    

    decoder:

    let decoder = JSONDecoder()
    parsedData = decoder.decode(YourStructure.self, from: YourJsonData)
    

    encode:

    let jsonEncoder = JSONEncoder()
    let jsonData = try jsonEncoder.encode(data)
    

    more info: Encoding and Decoding Custom Types

提交回复
热议问题