我正在尝试实现一个字典数组,但我得到的输出与我的预期不同。
在下面的代码片段中,我创建了一个数组,一个字典并执行了追加操作:
var MemberArray = [[String: Any]]()
let dict = ["member_status":"1",
"member_id": memid ,
"membership_number": memshpid,
"name": memname,
"mobile":memno ,
"billing":"1"] as NSDictionary
MemberArray.append(dict as! [String : Any])
我需要这样做:
[
{
"member_status": 1,
"member_id": 3,
"membership_number": "GI99010286",
"name": "Thomas",
"mobile": "9873684678",
"billing": 0
},
{
"member_status": 1,
"member_id": 5,
"membership_number": "GI99010144",
"name": "Raj",
"mobile": "9873684678",
"billing": 1
}
]
但我得到以下内容:
[
[
"member_status": 1,
"member_id": 3,
"membership_number": "GI99010286",
"name": "Thomas",
"mobile": "9873684678",
"billing": 0
],
[
"member_status": 1,
"member_id": 5,
"membership_number": "GI99010144",
"name": "Raj",
"mobile": "9873684678",
"billing": 1
]]
我如何实现目标?
答案 0 :(得分:4)
为此,您必须序列化该数组。这里的数据就是那个数组:
let dataSet = try! JSONSerialization.data(withJSONObject: data, options: JSONSerialization.WritingOptions.prettyPrinted)
let jsonString = NSString(data: dataSet, encoding: String.Encoding.utf8.rawValue)!