Swift3 数组转换为字符串,如"{"类型\ ": " 0\ "}"



我在 Swift 中创建了一个数组。我有一个节点.js API,在此 API 中仅传递这样的字符串类型数据

let Dic = "{"type": "0","message": "(message)", "sender_id":"(username)", "reciever_id": "61", "(username)": "2017-05-09 03:02:55 PM"}"

我无法在 API 中传递此Dic。但问题是,我有一个数组如何将数组转换为字符串.我必须在Dic中添加另一个参数,然后将这个数组传递给这是我的代码,我是如何生成这个数组的

(
{
Desc = "This is suger for m23 type ";
Price = 50;
Product = "Suger temp";
},
{
Desc = "This is suger for m23 type ";
Price = 50;
Product = "Suger temp";
}
)

代码是

let ary_allPRoduct :NSMutableArray = []
for i in 0..<count {
var dict = Dictionary<String, Any>()
dict["Product"]="Suger temp"
dict["Desc"]="This is suger for m23 type "
dict["Price"]="50"
ary_allPRoduct.add(dict)
}
print(ary_allPRoduct)

在 Swift 中,请应用此代码。

let jsonData = try? JSONSerialization.data(withJSONObject: dictionary, options: JSONSerialization.WritingOptions())
let jsonString = NSString(data: jsonData!, encoding: String.Encoding.utf8.rawValue)
print(jsonString)

注意:在控制台上,它通常会打印字符串。但是如果你在断点上检查它。您将看到所需的确切格式,它应该可以工作。

在控制台上:({"nacho":["1","2","3"]}(

和 它在断点上的外观

最新更新