最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

[已解决]swift把字典变量转换为JSON字符串

iOS crifan 808浏览 0评论

对于:

<code>           var chatListDict = [String:AnyObject]()
            chatListDict[&quot;user&quot;] = userIdTimeDict
            chatListDict[&quot;group&quot;] = groupIdTimeDict
            gLog.verbose(&quot;chatListDict=\(chatListDict)&quot;)
            /*
            chatListDict=[&quot;user&quot;: {
                &quot;user-00000000-0000-0000-0000-000000000002&quot; = 1461413085184;
                &quot;user-23fadd4f-806e-49a6-9957-4bcf38ca887a&quot; = 1461569454080;
                &quot;user-2cc1743f-2312-401c-9af3-1e5f6f1a5d72&quot; = 1460369489920;
                &quot;user-885333dd-4f47-41c0-b46f-4099d17e2153&quot; = 1460551417856;
                &quot;user-a6667201-472b-455c-9640-57c8f0b7e1cf&quot; = 1461679030272;
                &quot;user-bcc82fa9-4c7e-4dee-a905-6eac67fad865&quot; = 1461989670912;
                &quot;user-d02b6425-fc6a-4a1e-911b-c67dd7475a6c&quot; = 1461238759424;
                &quot;user-d248f799-c183-4858-9335-a57d59ddbfb6&quot; = 1461239021568;
                &quot;user-d25bb8d1-b16d-4089-a820-34490dfa81fc&quot; = 1461930819584;
                &quot;user-e201ca43-3c30-4720-b9a6-61f2f9e3b6d5&quot; = 1461835530240;
            }, &quot;group&quot;: {
                &quot;group-9ddd625b-cddc-4e32-a44f-a304274001d4&quot; = 1461811281920;
                &quot;group-a50ea3e7-41d1-4cda-b775-e3fab36aad1a&quot; = 1461912076288;
                &quot;group-c353d678-762c-4ca7-9985-086907be0ce7&quot; = 1461844180992;
                &quot;group-d1587572-b92d-4f5a-b301-e1f72ac5512b&quot; = 1461932392448;
                &quot;group-f044b665-1a16-4852-bb44-e0aa3173ea9f&quot; = 1461988491264;
                &quot;group-f9d0c845-51da-4127-826c-a4c47523a24d&quot; = 1461844312064;
            }]
             */
           
            let chatListDictStr = String(chatListDict)
            gLog.verbose(&quot;chatListDictStr=\(chatListDictStr)&quot;)
            /*
            chatListDictStr=[&quot;user&quot;: {
                &quot;user-00000000-0000-0000-0000-000000000002&quot; = 1461413085184;
                &quot;user-23fadd4f-806e-49a6-9957-4bcf38ca887a&quot; = 1461569454080;
                &quot;user-2cc1743f-2312-401c-9af3-1e5f6f1a5d72&quot; = 1460369489920;
                &quot;user-885333dd-4f47-41c0-b46f-4099d17e2153&quot; = 1460551417856;
                &quot;user-a6667201-472b-455c-9640-57c8f0b7e1cf&quot; = 1461679030272;
                &quot;user-bcc82fa9-4c7e-4dee-a905-6eac67fad865&quot; = 1461989670912;
                &quot;user-d02b6425-fc6a-4a1e-911b-c67dd7475a6c&quot; = 1461238759424;
                &quot;user-d248f799-c183-4858-9335-a57d59ddbfb6&quot; = 1461239021568;
                &quot;user-d25bb8d1-b16d-4089-a820-34490dfa81fc&quot; = 1461930819584;
                &quot;user-e201ca43-3c30-4720-b9a6-61f2f9e3b6d5&quot; = 1461835530240;
            }, &quot;group&quot;: {
                &quot;group-9ddd625b-cddc-4e32-a44f-a304274001d4&quot; = 1461811281920;
                &quot;group-a50ea3e7-41d1-4cda-b775-e3fab36aad1a&quot; = 1461912076288;
                &quot;group-c353d678-762c-4ca7-9985-086907be0ce7&quot; = 1461844180992;
                &quot;group-d1587572-b92d-4f5a-b301-e1f72ac5512b&quot; = 1461932392448;
                &quot;group-f044b665-1a16-4852-bb44-e0aa3173ea9f&quot; = 1461988491264;
                &quot;group-f9d0c845-51da-4127-826c-a4c47523a24d&quot; = 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 = &quot;\(chatListJson)&quot;
            gLog.verbose(&quot;chatListDictJsonStr=\(chatListDictJsonStr)&quot;)
            /*
{
  &quot;user&quot; : {
    &quot;user-885333dd-4f47-41c0-b46f-4099d17e2153&quot; : 1460551417856,
    &quot;user-23fadd4f-806e-49a6-9957-4bcf38ca887a&quot; : 1461569454080,
    &quot;user-2cc1743f-2312-401c-9af3-1e5f6f1a5d72&quot; : 1460369489920,
    &quot;user-e201ca43-3c30-4720-b9a6-61f2f9e3b6d5&quot; : 1461835530240,
    &quot;user-00000000-0000-0000-0000-000000000002&quot; : 1461413085184,
    &quot;user-d248f799-c183-4858-9335-a57d59ddbfb6&quot; : 1461239021568,
    &quot;user-a6667201-472b-455c-9640-57c8f0b7e1cf&quot; : 1461679030272,
    &quot;user-d25bb8d1-b16d-4089-a820-34490dfa81fc&quot; : 1461930819584,
    &quot;user-d02b6425-fc6a-4a1e-911b-c67dd7475a6c&quot; : 1461238759424,
    &quot;user-bcc82fa9-4c7e-4dee-a905-6eac67fad865&quot; : 1462085091328
  },
  &quot;group&quot; : {
    &quot;group-a50ea3e7-41d1-4cda-b775-e3fab36aad1a&quot; : 1461912076288,
    &quot;group-f9d0c845-51da-4127-826c-a4c47523a24d&quot; : 1461844312064,
    &quot;group-f044b665-1a16-4852-bb44-e0aa3173ea9f&quot; : 1461988491264,
    &quot;group-9ddd625b-cddc-4e32-a44f-a304274001d4&quot; : 1461811281920,
    &quot;group-d1587572-b92d-4f5a-b301-e1f72ac5512b&quot; : 1461932392448,
    &quot;group-c353d678-762c-4ca7-9985-086907be0ce7&quot; : 1461844180992
  }
}
             */
</code>

转载请注明:在路上 » [已解决]swift把字典变量转换为JSON字符串

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
60 queries in 0.152 seconds, using 18.80MB memory