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)
}
}
प्रिंटों:
[ "उद्यान"]
आप एक शब्दकोश के बजाय एक सरणी होने पसंद करते हैं, का पालन ही विचार: तो शब्दकोश बनाने परिवर्तित।
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)
}
}
प्रिंटों:
{ "स्थान": "उद्यान"}