0
:JSONSerializationは、私は、サーバーからJSON文字列を受け付けておりますし、それはこのようなエラーを返しますが、PHPのparcer
[[{\"type\":\"action\",\"action\":\"courier_on_map\",\"text\":\"\\u0421\\u043c\\u043e\\u0442\\u0440\\u0435\\u0442\\u044c \\u043d\\u0430 \\u043a\\u0430\\u0440\\u0442\\u0435\"}]]
ウェブパーサは「JSON文字列は有効ですが、JSONデータは正確ではありません」と言います。 JSONSerializationは、しかし、言う:文字1
とエラーを返すの周りのオブジェクト内の値の
ない文字列キー。
コード:
func convertToNSDictionary() -> NSDictionary?
{
var string = self
string = string.replacingOccurrences(of: "[", with: "")
string = string.replacingOccurrences(of: "]", with: "")
if let data = string.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary
} catch {
print(error.localizedDescription)
}
}
return nil
}
プロジェクトでこの最高のライブラリを使うことができます。これは単純にあなたのコードになり、おそらく問題を解決するでしょう:https://github.com/SwiftyJSON/SwiftyJSON – ewan
あなたのコードは私にとって完璧に動作しています。あなたの文字列はオプションではないことを確認してください。 –