JSON has to be an array or a dictionary, yalnızca bir Dize olamaz.
Ben bunu da dize içeren bir dizi oluşturmak önermek: yerine bir dize olarak bu JSON gerekiyorsa
if let json = try? NSJSONSerialization.dataWithJSONObject(array, options: []) {
// here `json` is your JSON data
}
:
let array = ["garden"]
Sonra bu diziden bir JSON nesnesi oluşturmak veri bunu kullanabilirsiniz:
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)
}
}
Baskı:
[ "bahçe"]
Eğer bir sözlük ziyade bir dizi yapmak isterseniz, aynı fikri izleyin: dönüştürmek ardından sözlüğü oluşturun.
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)
}
}
Baskılar:
{ "konum": "bahçe"}