【发布时间】:2017-06-19 07:19:43
【问题描述】:
这是一个奇怪的问题。我正在尝试在 Swift 3.0 中将 String 转换为 Dictionary 但字典始终具有 nil 值。不知道为什么
这是我的字符串到字典转换的代码。
var receivedMessage = [String: String]()
var MessageReceived:String!
do
{
let jsonData = try JSONSerialization.data(withJSONObject: MessageReceived:String, options: .prettyPrinted)
let decoded = try JSONSerialization.jsonObject(with: jsonData, options: [])
if let dictFromJSON = decoded as? [String:String]
{
print("MessageReceived",dictFromJSON)
}
}
catch
{
print(error.localizedDescription)
}
而我这里receivedMessage的字符串就是这样的。
{"_id":15, "isMine":true, "isReaded":false, "msgId":"1485956711767", “msgText”:“okkk”,“myId”:“218”,“myJId”:“1101784499938497_218_147300”, "senderName":"Burea", "time":"7:15PM", "timeStamp":1485956711768, "userId":"203", "userJId":"1234840263264009_203_496216" }
我作为字典得到的输出是: 收到消息 [:]
好吧,请帮帮我。 谢谢
【问题讨论】:
-
receivedMessage已经是Dictionary而不是String。 -
根据您的代码,
receivedMessage也为空 -
它是 JSON 字符串而不是字典 @Nirav
-
是的,当我尝试转换 @in 时收到的消息字典为空
-
json 应该这样转换.. as? [字符串:AnyObject]
标签: ios string dictionary swift3