简体   繁体   English

在Swift中将JsonString int值转换为字符串

[英]Convert JsonString int values to string in Swift

{
 "value" : 1234,
 "value2" : 123456
}

convert it to 转换成

{
 "value" : "123456",
 "value2" : "123456"
}

I am using ObjectMapper class , my aim is to parseJSON to Mapper class . 我正在使用ObjectMapper类,我的目的是将JSON解析为Mapper类。 But now the response has changed due to certain need. 但是现在,由于某些需要,响应已更改。 Initially all the values were in string but now values are in other types. 最初,所有值都是字符串形式,但现在值是其他类型。 So is it better to make changes in whole application or there is some way that i could convert all the values to string type in a complex json. 因此,最好在整个应用程序中进行更改,或者以某种方式我可以将所有值转换为复杂json中的字符串类型。

thanks. 谢谢。

You can use Transform : 您可以使用Transform

/// A transform which converts JSON to `String`.
///
/// - warning: It doesn't gaurantee the original type when convert `String` to JSON.
struct StringTransform: TransformType {

  func transformFromJSON(_ value: Any?) -> String? {
    return value.flatMap(String.init(describing:))
  }

  func transformToJSON(_ value: String?) -> Any? {
    return value
  }

}

Usage: 用法:

func mapping(map: Map) {
  stringValue <- (map["value2"], StringTransform())
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM