将JSON对象映射到Swift类/结构 [英] Mapping a JSON object to a Swift class/struct

查看:97
本文介绍了将JSON对象映射到Swift类/结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要复制"一个从JSON中的远程Web API服务返回的条目.看起来像这样:

I need to "replicate" an entiry which is returned from a remote web API service in JSON. It looks like this:

{
  "field1": "some_id",
  "entity_name" = "Entity1"
  "field2": "some name",
  "details1": [{
    "field1": 11,
    "field2": "some value",
    "data": {
      "key1": "value1",
      "key2": "value2",
      "key3": "value3",
      // any other, unknown at compile time keys
    }
  }],
  "details2": {
    "field1": 13,
    "field2": "some value2"
  }
}

这是我的尝试:

struct Entity1 {
  struct Details1 {
    let field1: UInt32
    let field2: String
    let data: [String: String]
  }

  struct Details2 {
    let field1: UInt32
    let field2: String
  }

  let field1: String
  static let entityName = "Entity1"
  let field2: String
  let details1: [Details1]
  let details2: Details2 
}

  1. 为此目的使用结构而不是类是一个好主意 作为我的?
  2. 无论如何,我可以定义一个嵌套的结构或类吗? Details1并同时创建一个变量?
  1. Is it a good idea to use structs instead of classes for such a goal as mine?
  2. Can I anyhow define a nested struct or a class, say Details1 and create a variable of it at the same time?

赞:

//doesn't compile 
struct Entity1 {
  let details1: [Details1 { 
  let field1: UInt32
  let field2: String
  let data: [String: String]
}]

推荐答案

如果可以使用以下优秀的开放源代码库来处理Swift中JSON到Object的映射,可以使用以下任何一种方法:

You can use any if the following good open-source libraries available to handle the mapping of JSON to Object in Swift, take a look :

  • Mapper
  • ObjectMapper
  • JSONHelper
  • Argo
  • Unbox

每个人都有一个不错的入门教程.

Each one have nice a good tutorial for beginners.

关于structclass的主题,您可以考虑以下内容来自

Regarding the theme of struct or class, you can consider the following text from The Swift Programming Language documentation:

结构实例始终按值和类传递 实例始终通过引用传递.这意味着他们是 适合不同的任务.当您考虑数据时 项目所需的结构和功能,决定 是否应将每个数据构造定义为一个类还是一个 结构.

Structure instances are always passed by value, and class instances are always passed by reference. This means that they are suited to different kinds of tasks. As you consider the data constructs and functionality that you need for a project, decide whether each data construct should be defined as a class or as a structure.

作为一般准则,请考虑在一个或多个时创建结构 以下条件适用:

As a general guideline, consider creating a structure when one or more of these conditions apply:

  • 该结构的主要目的是封装一些相对简单的数据值.
  • 可以合理地预期,当您分配或传递一个封装时,封装的值将被复制而不是被引用. 该结构的实例.
  • 该结构存储的任何属性本身就是值类型,也应该将其复制而不是引用.
  • 该结构无需继承其他现有类型的属性或行为.
  • The structure’s primary purpose is to encapsulate a few relatively simple data values.
  • It is reasonable to expect that the encapsulated values will be copied rather than referenced when you assign or pass around an instance of that structure.
  • Any properties stored by the structure are themselves value types, which would also be expected to be copied rather than referenced.
  • The structure does not need to inherit properties or behavior from another existing type.

结构良好的候选人包括:

Examples of good candidates for structures include:

  • 两个Double类型的几何形状的大小,可能封装了width属性和height属性.
  • 一种引用系列中范围的方法,可能封装了一个Int类型的start属性和一个length属性.
  • 3D坐标系中的一个点,可能封装了x,y和z属性,每个属性都是Double类型.
  • The size of a geometric shape, perhaps encapsulating a width property and a height property, both of type Double.
  • A way to refer to ranges within a series, perhaps encapsulating a start property and a length property, both of type Int.
  • A point in a 3D coordinate system, perhaps encapsulating x, y and z properties, each of type Double.

在所有其他情况下,定义一个类并创建该类的实例 通过引用进行管理和传递.实际上,这意味着 大多数自定义数据构造应该是类,而不是结构.

In all other cases, define a class, and create instances of that class to be managed and passed by reference. In practice, this means that most custom data constructs should be classes, not structures.

希望这对您有所帮助.

这篇关于将JSON对象映射到Swift类/结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆