跳转至

Core-fetchJSONString

/// 获取指定路径的原始json字符串
///
/// - Parameters:
///   - path: JSON数据路径 (默认为根路径)
///   - keys: 目标数据子路径  (例: [0, "_id"])
/// - Returns: 指定路径的原始json字符串
func fetchJSONString(path: String? = nil, keys: [JSONSubscriptType] = []) -> String

Response 转换为 String

  • path 指定取值路径,可以设置多级,如 retBody>name ,如果为 nil ,取根路径 ""
  • keypath的前提下,再指定详细的取值路径,可为 IntString

ps: 当前ResponseModelableParameterType 可以通过 MoyaMapperPlugin 进行统一设置


Example

success-obj

  • ""
let json = response.fetchJSONString()
print("json -- \(json)")
json -- {
  "retStatus" : "000",
  "retMsg" : "获取数据成功",
  "retBody" : {
    "name" : "MoyaMapper",
    "organization" : "https:\/\/github.com\/MoyaMapper",
    "github" : "https:\/\/github.com\/LinXunFeng",
    "avatar" : "https:\/\/avatars0.githubusercontent.com\/u\/19367531?s=460&v=4",
    "age" : 23,
    "author" : "LinXunFeng",
    "logoUrl" : "https:\/\/github.com\/MoyaMapper\/MoyaMapper.github.io\/raw\/master\/img\/MoyaMapper.png"
  }
}
  • "retBody"
let json = response.fetchJSONString(path: "retBody", keys: [])
print("json -- \(json)")
json -- {
  "name" : "MoyaMapper",
  "organization" : "https:\/\/github.com\/MoyaMapper",
  "github" : "https:\/\/github.com\/LinXunFeng",
  "avatar" : "https:\/\/avatars0.githubusercontent.com\/u\/19367531?s=460&v=4",
  "age" : 23,
  "author" : "LinXunFeng",
  "logoUrl" : "https:\/\/github.com\/MoyaMapper\/MoyaMapper.github.io\/raw\/master\/img\/MoyaMapper.png"
}