使用Retrofit创建动态对象

时间:2017-01-10 13:55:42

标签: android retrofit

我对self.mapView.isZoomEnabled = false let start = CGPoint(x: oldX, y: oldY) let end = CGPoint(x: newX, y: newY) print("old point is \(start)") self.mapView.addDashedLine(fromPoint: start, toPoint: end) self.mapView.isZoomEnabled = true 库并不熟练,我遇到了一个问题,我不知道如何在不重组整个项目的情况下处理。

基本上,我有两个非常相似的响应,但我的代码只处理一个响应。

以下是回复和我的所作所为,请告诉我是否有办法做到这一点......

Retrofit

好的,这只是一个示例,但您可以清楚地看到这些是具有其类型的一些文档。有17种类型的文档,每种文档都有不同的字段。

从逻辑上讲,我创建了17种不同的模型{ "cv":[ { "id":46, "name":"Ciriculum Vitae", "description":"Lorem ipsum description is the best description one can write down", "file":"1482915089-test-test-cv1.pdf", "file_url":"http://xxxyy/file/46/1482915089-test-test-cv1.pdf", "type":"cv" }, ... ], "diploma":[ { "id":52, "name":"dasdasdasdsa", "description":"Random description", "institution_name":"hello", "completed_date":"12.12.2016.", "file":"1482918005-test-test-dasdasdasdsa.pdf", "file_url":"http://xxxyy/file/52/1482918005-test-test-dasdasdasdsa.pdf", "type":"diploma" } ], ... "certification":[ { "id":50, "name":"Certificate of Greatness", "description":"I have been great at many things so everybody diecided to give me a certificate for it.", "institution_name":"Certification 3", "validation_date":"10.06.2017.", "file":"1482917772-test-test-dasdasdasdsa.pdf", "file_url":"http://xxxyy/file/50/1482917772-test-test-dasdasdasdsa.pdf", "type":"certification" } ], ... } 模型,cv模型等等。

当我想要获取与单个候选人相关的文档时,问题出现在项目的后期,那么响应是这样的:

diploma

Response2

现在显然是字段'类型'是需要创建的对象的类型。但到目前为止,我还不知道如何让我的模型适应第二个回应。

我该怎么办?伙计们?

如果您想知道我的代码是什么样的,这里就是......

 "documents": [
      {
        "id": 46,
        "name": "Ciriculum Vitae",
        "type": "cv",
        "description": "Lorem ipsum description is the best description one can write down",
        "file": "1482915089-test-test-cv1.pdf",
        "file_url": "http://xxxyy/file/46/1482915089-test-test-cv1.pdf"
      },
      {
         "id":52,
         "name":"dasdasdasdsa",
         "description":"Random description",
         "institution_name":"hello",
         "completed_date":"12.12.2016.",
         "file":"1482918005-test-test-dasdasdasdsa.pdf",
         "file_url":"http://xxxyy/file/52/1482918005-test-test-dasdasdasdsa.pdf",
         "type":"diploma"
      },
      {
        "id": 50,
        "name": "Certificate of Greatness",
        "type": "certification",
        "description": "I have been great at many things so everybody diecided to give me a certificate for it.",
        "file": "1482917772-test-test-dasdasdasdsa.pdf",
        "file_url": "http://xxxyy/file/50/1482917772-test-test-dasdasdasdsa.pdf"
      }
    ]
  }

修改 实际上我想要做的是创建依赖于'类型的对象。参数并用响应中的信息填充它。

我将如何做到这一点?

0 个答案:

没有答案