对于:
<code> var chatListDict = [String:AnyObject]() chatListDict["user"] = userIdTimeDict chatListDict["group"] = groupIdTimeDict gLog.verbose("chatListDict=\(chatListDict)") /* chatListDict=["user": { "user-00000000-0000-0000-0000-000000000002" = 1461413085184; "user-23fadd4f-806e-49a6-9957-4bcf38ca887a" = 1461569454080; "user-2cc1743f-2312-401c-9af3-1e5f6f1a5d72" = 1460369489920; "user-885333dd-4f47-41c0-b46f-4099d17e2153" = 1460551417856; "user-a6667201-472b-455c-9640-57c8f0b7e1cf" = 1461679030272; "user-bcc82fa9-4c7e-4dee-a905-6eac67fad865" = 1461989670912; "user-d02b6425-fc6a-4a1e-911b-c67dd7475a6c" = 1461238759424; "user-d248f799-c183-4858-9335-a57d59ddbfb6" = 1461239021568; "user-d25bb8d1-b16d-4089-a820-34490dfa81fc" = 1461930819584; "user-e201ca43-3c30-4720-b9a6-61f2f9e3b6d5" = 1461835530240; }, "group": { "group-9ddd625b-cddc-4e32-a44f-a304274001d4" = 1461811281920; "group-a50ea3e7-41d1-4cda-b775-e3fab36aad1a" = 1461912076288; "group-c353d678-762c-4ca7-9985-086907be0ce7" = 1461844180992; "group-d1587572-b92d-4f5a-b301-e1f72ac5512b" = 1461932392448; "group-f044b665-1a16-4852-bb44-e0aa3173ea9f" = 1461988491264; "group-f9d0c845-51da-4127-826c-a4c47523a24d" = 1461844312064; }] */ let chatListDictStr = String(chatListDict) gLog.verbose("chatListDictStr=\(chatListDictStr)") /* chatListDictStr=["user": { "user-00000000-0000-0000-0000-000000000002" = 1461413085184; "user-23fadd4f-806e-49a6-9957-4bcf38ca887a" = 1461569454080; "user-2cc1743f-2312-401c-9af3-1e5f6f1a5d72" = 1460369489920; "user-885333dd-4f47-41c0-b46f-4099d17e2153" = 1460551417856; "user-a6667201-472b-455c-9640-57c8f0b7e1cf" = 1461679030272; "user-bcc82fa9-4c7e-4dee-a905-6eac67fad865" = 1461989670912; "user-d02b6425-fc6a-4a1e-911b-c67dd7475a6c" = 1461238759424; "user-d248f799-c183-4858-9335-a57d59ddbfb6" = 1461239021568; "user-d25bb8d1-b16d-4089-a820-34490dfa81fc" = 1461930819584; "user-e201ca43-3c30-4720-b9a6-61f2f9e3b6d5" = 1461835530240; }, "group": { "group-9ddd625b-cddc-4e32-a44f-a304274001d4" = 1461811281920; "group-a50ea3e7-41d1-4cda-b775-e3fab36aad1a" = 1461912076288; "group-c353d678-762c-4ca7-9985-086907be0ce7" = 1461844180992; "group-d1587572-b92d-4f5a-b301-e1f72ac5512b" = 1461932392448; "group-f044b665-1a16-4852-bb44-e0aa3173ea9f" = 1461988491264; "group-f9d0c845-51da-4127-826c-a4c47523a24d" = 1461844312064; }] */ </code>
需要去把Dict变量chatListDict,转换为JSON字符串
swift dict to json string
ios – NSDictionary to json string to json object using SwiftyJSON – Stack Overflow
最后是去利用SwiftyJSON,自带转换为json字符串:
<code> let chatListJson = JSON(chatListDict) let chatListDictJsonStr = "\(chatListJson)" gLog.verbose("chatListDictJsonStr=\(chatListDictJsonStr)") /* { "user" : { "user-885333dd-4f47-41c0-b46f-4099d17e2153" : 1460551417856, "user-23fadd4f-806e-49a6-9957-4bcf38ca887a" : 1461569454080, "user-2cc1743f-2312-401c-9af3-1e5f6f1a5d72" : 1460369489920, "user-e201ca43-3c30-4720-b9a6-61f2f9e3b6d5" : 1461835530240, "user-00000000-0000-0000-0000-000000000002" : 1461413085184, "user-d248f799-c183-4858-9335-a57d59ddbfb6" : 1461239021568, "user-a6667201-472b-455c-9640-57c8f0b7e1cf" : 1461679030272, "user-d25bb8d1-b16d-4089-a820-34490dfa81fc" : 1461930819584, "user-d02b6425-fc6a-4a1e-911b-c67dd7475a6c" : 1461238759424, "user-bcc82fa9-4c7e-4dee-a905-6eac67fad865" : 1462085091328 }, "group" : { "group-a50ea3e7-41d1-4cda-b775-e3fab36aad1a" : 1461912076288, "group-f9d0c845-51da-4127-826c-a4c47523a24d" : 1461844312064, "group-f044b665-1a16-4852-bb44-e0aa3173ea9f" : 1461988491264, "group-9ddd625b-cddc-4e32-a44f-a304274001d4" : 1461811281920, "group-d1587572-b92d-4f5a-b301-e1f72ac5512b" : 1461932392448, "group-c353d678-762c-4ca7-9985-086907be0ce7" : 1461844180992 } } */ </code>
转载请注明:在路上 » [已解决]swift把字典变量转换为JSON字符串