2017-10-06 12 views
-1

Nous avons une charge utile JSON:Comment convertir une charge utile JSON en objet personnalisé?

{ 
    "aps": { 
    "alert": { 
     "title": "Payload", 
     "body": "Lets map this thing" 
    }, 
    }, 
    "type": "alert", 
    "message": "This is a message", 
} 

L'objet personnalisé a été créé:

class PushNotificationDetail { 

var title: String //operation 
var body: String //message 
var type: detailType 
var message: String? 

init(title: String, body: String, type: detailType, message: string?){ 

    self.title = title 
    self.body = body 
    self.type = type 
    self.message = message 
} 
} 

Le problème est la cartographie correctement à l'objet créé, quelle serait la meilleure façon d'y parvenir?

+0

https://www.raywenderlich.com/ 150322/swift-json-tutorial-2 ou https://developer.apple.com/swift/blog/?id=37 – CodeNinja

+0

Je pense que vous devriez mettre le titre et le corps à l'intérieur du modèle nommé 'alert' ou' detailType' (I thi nk vous utilisez 'detailType', d'ailleurs vous devriez nommer vos types avec des lettres majuscules, par exemple DetailType) – 3stud1ant3

+1

Quel est le problème que vous rencontrez avec le mapping? Votre question ne montre aucune tentative de mappage pour tenter d'analyser le JSON. – rmaddy

Répondre

1

Vous devez utiliser le protocole Swift4 Codable pour initialiser votre objet à partir du json renvoyé par l'API. Vous aurez besoin de restructurer votre structure pour faire correspondre les données renvoyées par le api:

struct PushNotificationDetail: Codable, CustomStringConvertible { 
    let aps: Aps 
    let type: String 
    let message: String? 
    var description: String { return aps.description + " - Type: " + type + " - Message: " + (message ?? "") } 
} 
struct Aps: Codable, CustomStringConvertible { 
    let alert: Alert 
    var description: String { return alert.description } 
} 
struct Alert: Codable, CustomStringConvertible { 
    let title: String 
    let body: String 
    var description: String { return "Tile: " + title + " - " + "Body: " + body } 
} 

extension Data { 
    var string: String { return String(data: self, encoding: .utf8) ?? "" } 
} 

Test Playground

let json = """ 
{"aps":{"alert":{"title":"Payload","body":"Lets map this thing"}},"type":"alert","message":"This is a message"} 
""" 

if let pnd = try? JSONDecoder().decode(PushNotificationDetail.self, from: Data(json.utf8)) { 
    print(pnd) // "Tile: Payload - Body: Lets map this thing - Type: alert - Message: This is a message\n" 
    // lets encode it 
    if let data = try? JSONEncoder().encode(pnd) { 
     print(data.string) // "{"aps":{"alert":{"title":"Payload","body":"Lets map this thing"}},"type":"alert","message":"This is a message"}\n" 
     print(data == Data(json.utf8)) // true 
    } 
} 
0

Vous pouvez faire cela avec un failable dans votre initialiseur classe PushNotificationDetail et une déclaration de garde enchaîné:

init?(jsonDict: [String : Any]) { 

    guard let typeString : String = jsonDict[“type”] as? String, 
     let message: String = jsonDict[“message”] as? String, 
     let aps : [String : Any] = jsonDict[“aps”] as? [String : Any], 
     let alert : [String : String] = aps[“alert”] as? [String : String], 
     let title : String = alert[“title”], 
     let body : String = alert[“body”] else { return nil } 

    // implement some code here to create type from typeString 

    self.init(title: title, body: body, type: type, message: message) 

} 

espoir qui aide.