JSON has to be an array or a dictionaryは、文字列だけではありません。
私はあなたがそれであなたの文字列の配列を作成してお勧め:
let array = ["garden"]
次にあなたがこの配列からJSONオブジェクトを作成します。
if let json = try? NSJSONSerialization.dataWithJSONObject(array, options: []) {
// here `json` is your JSON data
}
を文字列として、このJSONが必要な場合は代わりに
if let json = try? NSJSONSerialization.dataWithJSONObject(array, options: []) {
// here `json` is your JSON data, an array containing the String
// if you need a JSON string instead of data, then do this:
if let content = String(data: json, encoding: NSUTF8StringEncoding) {
// here `content` is the JSON data decoded as a String
print(content)
}
}
プリント:
データあなたはこれを使用することができますそれを変換し、その後辞書を作成する:あなたは辞書ではなく、アレイを有することを好む場合は3210
[「庭」]
、同じ考えに従ってください。
let dict = ["location": "garden"]
if let json = try? NSJSONSerialization.dataWithJSONObject(dict, options: []) {
if let content = String(data: json, encoding: NSUTF8StringEncoding) {
// here `content` is the JSON dictionary containing the String
print(content)
}
}
プリント:
{ "場所": "庭園"}