如何将Swift对象序列化或转换为JSON?

5ktev3wc  于 2023-02-11  发布在  Swift
关注(0)|答案(9)|浏览(395)

这低于类

class User: NSManagedObject {
  @NSManaged var id: Int
  @NSManaged var name: String
}

需要转换为

{
    "id" : 98,
    "name" : "Jon Doe"
}

我试着手动将对象传递给一个函数,该函数将变量设置到字典中并返回字典,但我希望有一个更好的方法来实现这一点。

oxosxuxt

oxosxuxt1#

在Swift 4中,可以从Codable类型继承。

struct Dog: Codable {
    var name: String
    var owner: String
}

// Encode
let dog = Dog(name: "Rex", owner: "Etgar")

let jsonEncoder = JSONEncoder()
let jsonData = try jsonEncoder.encode(dog)
let json = String(data: jsonData, encoding: String.Encoding.utf8)

// Decode
let jsonDecoder = JSONDecoder()
let secondDog = try jsonDecoder.decode(Dog.self, from: jsonData)
qlfbtfca

qlfbtfca2#

沿着Swift 4(Foundation)的推出,现在它在两种方式上都得到了原生支持,即JSON字符串到对象-对象到JSON字符串。请参阅Apple文档JSONDecoder()JSONEncoder()
JSON字符串到对象

let jsonData = jsonString.data(using: .utf8)!
let decoder = JSONDecoder()
let myStruct = try! decoder.decode(myStruct.self, from: jsonData)

Swift对象到JSON字符串

let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let data = try! encoder.encode(myStruct)
print(String(data: data, encoding: .utf8)!)

您可以在此处找到所有详细信息和示例Ultimate Guide to JSON Parsing With Swift 4

cbwuti44

cbwuti443#

    • 更新:**Swift 4中引入的Codable协议应该足以满足大多数JSON解析情况。以下答案是针对那些因遗留问题而无法使用Swift早期版本的用户
  • 它采用反射原理,代码量少,支持NSDictionaryNSCodingPrintableHashableEquatable
    • 示例:**
class User: EVObject { # extend EVObject method for the class
       var id: Int = 0
       var name: String = ""
       var friends: [User]? = []
    }

    # use like below
    let json:String = "{\"id\": 24, \"name\": \"Bob Jefferson\", \"friends\": [{\"id\": 29, \"name\": \"Jen Jackson\"}]}"
    let user = User(json: json)
    • 一个电子一个电子一个电子:**
  • 另一种方法是使用ObjectMapper,这提供了更多的控制,但也需要更多的代码。
    • 示例:**
class User: Mappable { # extend Mappable method for the class
       var id: Int?
       var name: String?

       required init?(_ map: Map) {

       }

       func mapping(map: Map) { # write mapping code
          name    <- map["name"]
          id      <- map["id"]
       }

    }

    # use like below
    let json:String = "{\"id\": 24, \"name\": \"Bob Jefferson\", \"friends\": [{\"id\": 29, \"name\": \"Jen Jackson\"}]}"
    let user = Mapper<User>().map(json)
ctehm74n

ctehm74n4#

我曾经研究过一个不需要继承的小解决方案,但是它还没有经过很多测试,它是相当丑陋的atm。
https://github.com/peheje/JsonSerializerSwift
你可以把它放到操场上测试一下。例如,下面的班级结构:

//Test nonsense data
class Nutrient {
    var name = "VitaminD"
    var amountUg = 4.2

    var intArray = [1, 5, 9]
    var stringArray = ["nutrients", "are", "important"]
}

class Fruit {
    var name: String = "Apple"
    var color: String? = nil
    var weight: Double = 2.1
    var diameter: Float = 4.3
    var radius: Double? = nil
    var isDelicious: Bool = true
    var isRound: Bool? = nil
    var nullString: String? = nil
    var date = NSDate()

    var optionalIntArray: Array<Int?> = [1, 5, 3, 4, nil, 6]
    var doubleArray: Array<Double?> = [nil, 2.2, 3.3, 4.4]
    var stringArray: Array<String> = ["one", "two", "three", "four"]
    var optionalArray: Array<Int> = [2, 4, 1]

    var nutrient = Nutrient()
}

var fruit = Fruit()
var json = JSONSerializer.toJson(fruit)

print(json)

印刷品

{"name": "Apple", "color": null, "weight": 2.1, "diameter": 4.3, "radius": null, "isDelicious": true, "isRound": null, "nullString": null, "date": "2015-06-19 22:39:20 +0000", "optionalIntArray": [1, 5, 3, 4, null, 6], "doubleArray": [null, 2.2, 3.3, 4.4], "stringArray": ["one", "two", "three", "four"], "optionalArray": [2, 4, 1], "nutrient": {"name": "VitaminD", "amountUg": 4.2, "intArray": [1, 5, 9], "stringArray": ["nutrients", "are", "important"]}}
cwxwcias

cwxwcias5#

这不是一个完美的/自动的解决方案,但我相信这是一个惯用的和本地的方法,这样你就不需要任何库或类似的东西了。
创建协议,例如:

/// A generic protocol for creating objects which can be converted to JSON
protocol JSONSerializable {
    private var dict: [String: Any] { get }
}

extension JSONSerializable {
    /// Converts a JSONSerializable conforming class to a JSON object.
    func json() rethrows -> Data {
        try JSONSerialization.data(withJSONObject: self.dict, options: nil)
    }
}

然后在类中实现它,例如:

class User: JSONSerializable {
    var id: Int
    var name: String

    var dict { return ["id": self.id, "name": self.name]  }
}

现在:

let user = User(...)
let json = user.json()

注意:如果你想要json作为一个字符串,它是非常简单的转换成一个字符串:String(data: json, encoding .utf8)

btqmn9zl

btqmn9zl6#

上面的一些答案是完全正确的,但是我在这里添加了一个扩展,只是为了使它更具可读性和可用性。

extension Encodable {
    var convertToString: String? {
        let jsonEncoder = JSONEncoder()
        jsonEncoder.outputFormatting = .prettyPrinted
        do {
            let jsonData = try jsonEncoder.encode(self)
            return String(data: jsonData, encoding: .utf8)
        } catch {
            return nil
        }
    }
}

struct User: Codable {
     var id: Int
     var name: String
}

let user = User(id: 1, name: "name")
print(user.convertToString!)

//这将打印如下:

{
  "id" : 1,
  "name" : "name"
}
e7arh2l6

e7arh2l67#

不确定lib/framework是否存在,但如果您希望自动完成,并且希望避免手工劳动:-)坚持使用MirrorType...

class U {

  var id: Int
  var name: String

  init(id: Int, name: String) {
    self.id = id
    self.name = name
  }

}

extension U {

  func JSONDictionary() -> Dictionary<String, Any> {
    var dict = Dictionary<String, Any>()

    let mirror = reflect(self)

    var i: Int
    for i = 0 ; i < mirror.count ; i++ {
      let (childName, childMirror) = mirror[i]

      // Just an example how to check type
      if childMirror.valueType is String.Type {
        dict[childName] = childMirror.value
      } else if childMirror.valueType is Int.Type {
        // Convert to NSNumber for example
        dict[childName] = childMirror.value
      }
    }

    return dict
  }

}

把它作为一个粗略的例子,缺乏适当的转换支持,缺乏递归,...它只是MirrorType演示...
P.S.这里是在U中完成的,但是你将增强NSManagedObject,然后你将能够转换所有NSManagedObject子类。没有必要在所有子类/托管对象中实现这一点。

dgsult0t

dgsult0t8#

struct User:Codable{
 var id:String?
 var name:String?
 init(_ id:String,_ name:String){
   self.id  = id
   self.name = name
 }
}

现在把你的物体做成这样
让用户=用户(“1”,“pawan”)

do{
      let userJson =  try JSONEncoder().encode(parentMessage) 
            
    }catch{
         fatalError("Unable To Convert in Json")      
    }

然后从json重新转换为Object

let jsonDecoder = JSONDecoder()
do{
   let convertedUser = try jsonDecoder.decode(User.self, from: userJson.data(using: .utf8)!)
 }catch{
   
 }
xqkwcwgp

xqkwcwgp9#

2021年|SWIFT 5.1|结果溶液

输入数据:

struct ConfigCreds: Codable {
    // some params
}

用法:

// get JSON from Object
configCreds
   .asJson()
   .onSuccess{ varToSaveJson = $0 }
   .onFailure{ _ in // any failure code }

// get object of type "ConfigCreds" from JSON
someJsonString
    .decodeFromJson(type: ConfigCreds.self)
    .onSuccess { configCreds = $0 }
    .onFailure{ _ in // any failure code }

返回代码:

@available(macOS 10.15, *)
public extension Encodable {
    func asJson() -> Result<String, Error>{
        JSONEncoder()
            .try(self)
            .flatMap{ $0.asString() }
    }
}

public extension String {
    func decodeFromJson<T>(type: T.Type) -> Result<T, Error> where T: Decodable {
        self.asData()
            .flatMap { JSONDecoder().try(type, from: $0) }
    }
}

///////////////////////////////
/// HELPERS
//////////////////////////////

@available(macOS 10.15, *)
fileprivate extension JSONEncoder {
    func `try`<T : Encodable>(_ value: T) -> Result<Output, Error> {
        do {
            return .success(try self.encode(value))
        } catch {
            return .failure(error)
        }
    }
}

fileprivate extension JSONDecoder {
    func `try`<T: Decodable>(_ t: T.Type, from data: Data) -> Result<T,Error> {
        do {
            return .success(try self.decode(t, from: data))
        } catch {
            return .failure(error)
        }
    }
}

fileprivate extension String {
    func asData() -> Result<Data, Error> {
        if let data = self.data(using: .utf8) {
            return .success(data)
        } else {
            return .failure(WTF("can't convert string to data: \(self)"))
        }
    }
}

fileprivate extension Data {
    func asString() -> Result<String, Error> {
        if let str = String(data: self, encoding: .utf8) {
            return .success(str)
        } else {
            return .failure(WTF("can't convert Data to string"))
        }
    }
}

fileprivate func WTF(_ msg: String, code: Int = 0) -> Error {
    NSError(code: code, message: msg)
}

相关问题