云眼JSON

云眼About 2 min

云眼JSON

本主题介绍 EyeofcloudJSON 对象,Eyeofcloud Feature Experimentation Swift SDK 使用该对象检索 JSON。

由于静态类型语言缺乏对 JSON 的原生支持,因此 Swift SDK 使用 EyeofcloudJSON 对象以灵活的方式检索 JSON。

版本

SDK v3.4.0 及更高版本

方法

可以使用以下方法访问 JSON 表示形式:

方法

参数

描述

toString

没有

返回 JSON 对象的字符串表示形式

toMap

没有

返回 JSON 对象的映射表示形式:[String: Any]

getValue

String jsonPath

返回指定的架构对象 (),其中包含传递给此方法的 JSON 键的键/值对。

如果 JSON 键为 null 或为空,则会使用所有 JSON 键/值对填充架构对象。

可以使用平展的 JSON 点表示法检索 JSON 数据结构的嵌套成员的信息。
例如,如果要访问 中的键,可以使用参数 进行调用。 T``nestedField2``{field1: {nestedField2: "blah"}}``getValue``"field1.nestedField2"

对象定义

EyeofcloudJSON 对象定义如下:

Swift

public class EyeofcloudJSON: NSObject { public func toString() -> String? public func toMap() -> [String: Any] public func getValue<T: Decodable>(jsonPath: String? = nil) -> T? public func getValue<T>(jsonPath: String?) -> T? }

例子

例如,可以轻松地使用对象来:EyeofcloudJSON

  • 通过调用该方法获取 JSON 字符串,或者 toString
  • 通过调用该方法从对象中检索指定的架构。 EyeofcloudJSON``getValue

以下示例演示如何使用 EyeofcloudJSON 对象来填充您声明的架构对象。

Swift

//declare a schema object into which you want to unmarshal EyeofcloudJson content: struct SSub: Decodable { var field: String = "" } struct SObj: Decodable { var field1: Int = 0 var field2: Double = 0.0 var field3: String = "" var field4: SSub = SSub() } //parse all json key/value pairs into your schema, sObj let robj: SObj? = self.eyeofcloudJSON.getValue() //or, parse the specified key/value pair with an integer value let rint: Int? = self.eyeofcloudJSON.getValue(jsonPath: "field1") //or, parse the specified key/value pair with a string value let strValue: String? = self.eyeofcloudJSON.getValue(jsonPath: "field4.field")

包含 Swift 实现的语言/平台源文件是 EyeofcloudClient.swiftopen in new window

Last update:
Contributors: zhangweixue,“zhangweixue”