2015-11-02 67 views
0

我收到类似下面的JSON对象。我如何将它转换成Swift 2.1容易处理的格式?我将收到其中的几个,并且必须将它们放入一个数组中并通过createdAt进行排序。如何从JSON创建字典

Optional({ 
    comment = "<null>"; 
    completedAt = "<null>"; 
    createdAt = "2015-11-02 15:01:04 +0000"; 
    paid = 1; 
    paidAt = "2015-11-02 15:01:04 +0000"; 
    startedAt = "<null>"; 
    state = request; 
    type = doctor; 
    user = KTsCySacEAiz3eDnf; 
    userdata =  { 
     birthdate = "<null>"; 
     gender = "<null>"; 
    }; 
}) 
+0

[斯威夫特:额外的参数“错误”在电话]的可能的复制(http://stackoverflow.com/questions/31073497/swift-extra-argument-error在通话) – Moritz

+0

简单的例子,你在我的链接答案。 – Moritz

+0

嗨,我看了看,它给我[_]是不是可兑换到NSJSONReadingOptions – KML

回答

-1

正如你说你期待一个解释的标题,你可以得到一个通过简单serializate的JSON并将其转换为一个字典。

do{ 
    let json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary 
}catch{ 
    print("Something went wrong") 
} 

其中数据是你的json内容作为NSData。如果你得到一个字符串,你可以很容易转换

let data = text.dataUsingEncoding(NSUTF8StringEncoding) 
+0

我得到了使用未解决的已识别err – KML

+0

你是什么意思“Where data is you json content”是什么意思? – KML

+0

我现在在通话中收到'Extra argument'错误',...你确定这是swift 2.0吗? – KML