跳转至

Core-toJSON

/// Response -> JSON
///
/// - Parameter modelKey: 模型数据路径
/// - Returns: JSON对象
func toJSON(modelKey: String? = nil) -> JSON

Response 转换为 JSON

返回值 为 SwiftyJSON 的 JSON 对象


modelKey 用于指定特殊取值路径,在这里主要指定 ModelableParameterType 的 modelKey

如果为 nil ,则取当前 Response 的 ModelableParameterType 为取值依据

ps: 当前Response 的 ModelableParameterType 可以通过 MoyaMapperPlugin 进行统一设置

Example

success-obj

调用 toJSON 并传入需要指定的 modelKey

let json = response.toJSON(modelKey: "retBody")
let name = json["author"].stringValue
let age = json["age"].intValue
print("author -- \(name)")
print("age -- \(age)")

打印结果

author -- LinXunFeng
age -- 23