Swift 第三方JSON转模型对象利器

最新神器 JSONExport

Swift 汉化版 JSONExport

自动生成swift模型文件
可任意JSON字符串生成Swift对应的模型(类、结构体、Core Data类、Realm类、SwiftlyJSON类), 并可生成帮助方法(把实例转回NSDictionary)。

JSONUtilities -

      JSON对象和解码成结构体或者类

GitHub地址:https://github.com/lucianomarisi/JSONUtilities

D3Json -

     通过swift的反射特性,把json数据转换为model对象

GitHub地址:https://github.com/mozhenhau/D3Json

JSONUtilities使用

①直接对json解码

例如:个人信息的JSON数据

           { 
             "name" : "John Doe", 
             "age" : 24,
             "weight" : 72.4
           }

    let jsonDictionary = try JSONDictionary.fromFile("person") //获取json文件
    let name: String = try jsonDictionary.jsonKey("name")
    let age: Int = try jsonDictionary.jsonKey("age")
    let weight: Int = try jsonDictionary.jsonKey("weight")
    let profession: String? = jsonDictionary.jsonKey("profession") // 可选解码

②解码成结构体或类

    struct Person {
        
        let name : String
        let age : Int
        let weight : Double
        let profession : String?
        
        init(jsonDictionary: JSONDictionary) throws {
            name = try jsonDictionary.jsonKey("name")
            age = try jsonDictionary.jsonKey("age")
            weight = try jsonDictionary.jsonKey("weight")
            profession = jsonDictionary.jsonKey("profession")
        }
        
    }

    class Person {
        
        let name : String
        let age : Int
        let weight : Double
        let profession : String?
        
        init(name: String,
            age: Int,
            weight: Double
            profession: String?) {
                self.name = name
                self.age = age
                self.weight = weight
                self.profession = profession
        }
        
        //需要一个便利构造器初始化类,因为Swift不可以在throw设置初始值
        convenience init(jsonDictionary: JSONDictionary) throws {
            self.init(
                name : try jsonDictionary.jsonKey("name"),
                age : try jsonDictionary.jsonKey("age"),
                weight : try jsonDictionary.jsonKey("weight"),
                profession : try jsonDictionary.jsonKey("profession")
            )
        }
        
    }

③解码嵌套的结构体或类需要遵从可解码的协议

例如:公司员工信息的JSON对象

    {
        "name" : "Working name LTD.",
        "employees": [
        {
        "name": "John Doe",
        "age": 24,
        "weight": 72.4
        },
        {
        "name": "Jane Doe",
        "age": 22,
        "weight": 70.1
        }
        ]
    }

    struct Company {
        let name: String
        let employees: [Person]
        
        init(jsonDictionary: JSONDictionary) throws {
            name = try jsonDictionary.jsonKey("name")
            employees = try jsonDictionary.jsonKey("employees")
        }
    }

    //解码嵌套的结构体或类需要遵从可解码的协议
    struct Person:Decodable {
        let name : String
        let age : Int
        let weight : Double
        let profession : String?
        
        init(jsonDictionary: JSONDictionary) throws {
            name = try jsonDictionary.jsonKey("name")
            age = try jsonDictionary.jsonKey("age")
            weight = try jsonDictionary.jsonKey("weight")
            profession = jsonDictionary.jsonKey("profession")
        } 
    }

--

D3Json使用

①json数据转model

   var user:User = User.jsonToModel(json) //把json数据转换成User类

②json数据转换成Array类

     var users = User.jsonToModelList(jsonArr) as! Array

你可能感兴趣的:(Swift 第三方JSON转模型对象利器)