Swift2 从Xcode 7+中的Swift对象创建JSON字符串;

Swift2 从Xcode 7+中的Swift对象创建JSON字符串;,swift2,Swift2,我需要使用Xcode 7及更高版本将以下类转换为JSON字符串。在以前版本的Xcode中,有一个可用的jsonseralization.toJson(AnyObject)函数,但是在Xcode中没有出现 我需要转换以下类: struct ContactInfo { var contactDeviceType: String var contactNumber: String } class Tradesmen { var type:String = "" var

我需要使用Xcode 7及更高版本将以下类转换为JSON字符串。在以前版本的Xcode中,有一个可用的
jsonseralization.toJson(AnyObject)
函数,但是在Xcode中没有出现

我需要转换以下类:

struct ContactInfo
{
    var contactDeviceType: String
    var contactNumber: String
}

class Tradesmen
{
    var type:String = ""
    var name: String = ""
    var companyName: String = ""
    var contacts: [ContactInfo] = []

init(type:String, name:String, companyName:String, contacts [ContactInfo])
    {
        self.type = type
        self.name = name
        self.companyName = companyName
        self.contacts = contacts
    }
我已经设置了我的测试数据如下

contactType =
        [
           ContactInfo(contactDeviceType: "Home", contactNumber: "(604) 555-1111"),
           ContactInfo(contactDeviceType: "Cell", contactNumber: "(604) 555-2222"),
           ContactInfo(contactDeviceType: "Work", contactNumber: "(604) 555-3333")
        ]




var tradesmen = Tradesmen(type: "Plumber", name: "Jim Jones", companyName: "Jim Jones Plumbing", contacts: contactType)

任何帮助或指导都将不胜感激。

我不认为有任何直接的方法,即使是在以前的Xcode中。您需要编写自己的实现。如下所示:

protocol JSONRepresenatble {
    static func jsonArray(array : [Self]) -> String
    var jsonRepresentation : String {get}
}

extension JSONRepresenatble {
    static func jsonArray(array : [Self]) -> String {
        return "[" + array.map {$0.jsonRepresentation}.joinWithSeparator(",") + "]"
    }
}
struct ContactInfo: JSONRepresenatble {
    var contactDeviceType: String
    var contactNumber: String
    var jsonRepresentation: String {
        return "{\"contactDeviceType\": \"\(contactDeviceType)\", \"contactNumber\": \"\(contactNumber)\"}"
    }
}

struct Tradesmen: JSONRepresenatble {
    var type:String = ""
    var name: String = ""
    var companyName: String = ""
    var contacts: [ContactInfo] = []
    var jsonRepresentation: String {
        return "{\"type\": \"\(type)\", \"name\": \"\(name)\", \"companyName\": \"\(companyName)\", \"contacts\": \(ContactInfo.jsonArray(contacts))}"
    }
    init(type:String, name:String, companyName:String, contacts: [ContactInfo]) {
        self.type = type
        self.name = name
        self.companyName = companyName
        self.contacts = contacts
    }
}
然后在模态中实现JSONRepresentable,如下所示:

protocol JSONRepresenatble {
    static func jsonArray(array : [Self]) -> String
    var jsonRepresentation : String {get}
}

extension JSONRepresenatble {
    static func jsonArray(array : [Self]) -> String {
        return "[" + array.map {$0.jsonRepresentation}.joinWithSeparator(",") + "]"
    }
}
struct ContactInfo: JSONRepresenatble {
    var contactDeviceType: String
    var contactNumber: String
    var jsonRepresentation: String {
        return "{\"contactDeviceType\": \"\(contactDeviceType)\", \"contactNumber\": \"\(contactNumber)\"}"
    }
}

struct Tradesmen: JSONRepresenatble {
    var type:String = ""
    var name: String = ""
    var companyName: String = ""
    var contacts: [ContactInfo] = []
    var jsonRepresentation: String {
        return "{\"type\": \"\(type)\", \"name\": \"\(name)\", \"companyName\": \"\(companyName)\", \"contacts\": \(ContactInfo.jsonArray(contacts))}"
    }
    init(type:String, name:String, companyName:String, contacts: [ContactInfo]) {
        self.type = type
        self.name = name
        self.companyName = companyName
        self.contacts = contacts
    }
}

我不认为有任何直接的方法,即使是在以前的Xcode中。您需要编写自己的实现。如下所示:

protocol JSONRepresenatble {
    static func jsonArray(array : [Self]) -> String
    var jsonRepresentation : String {get}
}

extension JSONRepresenatble {
    static func jsonArray(array : [Self]) -> String {
        return "[" + array.map {$0.jsonRepresentation}.joinWithSeparator(",") + "]"
    }
}
struct ContactInfo: JSONRepresenatble {
    var contactDeviceType: String
    var contactNumber: String
    var jsonRepresentation: String {
        return "{\"contactDeviceType\": \"\(contactDeviceType)\", \"contactNumber\": \"\(contactNumber)\"}"
    }
}

struct Tradesmen: JSONRepresenatble {
    var type:String = ""
    var name: String = ""
    var companyName: String = ""
    var contacts: [ContactInfo] = []
    var jsonRepresentation: String {
        return "{\"type\": \"\(type)\", \"name\": \"\(name)\", \"companyName\": \"\(companyName)\", \"contacts\": \(ContactInfo.jsonArray(contacts))}"
    }
    init(type:String, name:String, companyName:String, contacts: [ContactInfo]) {
        self.type = type
        self.name = name
        self.companyName = companyName
        self.contacts = contacts
    }
}
然后在模态中实现JSONRepresentable,如下所示:

protocol JSONRepresenatble {
    static func jsonArray(array : [Self]) -> String
    var jsonRepresentation : String {get}
}

extension JSONRepresenatble {
    static func jsonArray(array : [Self]) -> String {
        return "[" + array.map {$0.jsonRepresentation}.joinWithSeparator(",") + "]"
    }
}
struct ContactInfo: JSONRepresenatble {
    var contactDeviceType: String
    var contactNumber: String
    var jsonRepresentation: String {
        return "{\"contactDeviceType\": \"\(contactDeviceType)\", \"contactNumber\": \"\(contactNumber)\"}"
    }
}

struct Tradesmen: JSONRepresenatble {
    var type:String = ""
    var name: String = ""
    var companyName: String = ""
    var contacts: [ContactInfo] = []
    var jsonRepresentation: String {
        return "{\"type\": \"\(type)\", \"name\": \"\(name)\", \"companyName\": \"\(companyName)\", \"contacts\": \(ContactInfo.jsonArray(contacts))}"
    }
    init(type:String, name:String, companyName:String, contacts: [ContactInfo]) {
        self.type = type
        self.name = name
        self.companyName = companyName
        self.contacts = contacts
    }
}