热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

如何使用包含已编码值的Encodable在Swift中编码结构

如何解决《如何使用包含已编码值的Encodable在Swift中编码结构》经验,求助如何解决?

想象一下一个数据结构,其中包含一个contents已经编码的JSON片段的值。

let partial = """
{ "foo": "Foo", "bar": 1 }
"""

struct Document {
  let contents: String
  let other: [String: Int]
}

let doc = Document(contents: partial, other: ["foo": 1])

所需的输出

组合的数据结构应contents原样使用并编码other

{
  "contents": { "foo": "Foo", "bar": 1 },
  "other": { "foo": 1 }
}

使用 Encodable

以下Encodable编码实现Document为JSON,但是也将其重新编码contents为字符串,这意味着它被包装在引号中,并且所有"引号都转义为\"

{
  "contents": { "foo": "Foo", "bar": 1 },
  "other": { "foo": 1 }
}

输出量

{
  "contents": "{\"foo\": \"Foo\", \"bar\": 1}",
  "other": { "foo": 1 }
}

怎样才能encodecontents原样通过?


推荐阅读
author-avatar
手机用户2502855763
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有