2017-10-18 4 views
0
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [.allowFragments]) 
     if let responseJSON = responseJSON as? [String:Any] { 
      if let tJsonObj = xResponse["d"] as? [[String:Any]] { 
       // not working here... 
      } 
     } 

La variable tJsonObj n'obtient pas mon contenu de tableau json. Mon JSON ressemble à ceci:Swift - Le tableau Parse de l'objet JSON imbriqué ne fonctionne pas

{"d": "[{\"title\":\"xxx\",\"timestamp\":\"2017-10-16 23:53:40\"},{\"title\":\"Mein Test iPhone 7\",\"timestamp\":\"2017-10-17 18:16:24\"}]"} 

J'espère que quelqu'un peut aider - merci!

+0

Jetez un oeil à: https://stackoverflow.com/questions/41734982/parsing-nested-array-of-dictionaries-using-object-mapper/41735194#41735194 –

Répondre

1

La valeur de la clé d est une autre chaîne JSON. Vous devez utiliser JSONSerialization deux fois

do { 
    if let responseJSON = try JSONSerialization.jsonObject(with: data) as? [String:Any], 
    let tJsonObj = responseJSON["d"] as? String { 
     if let innerJSON = try JSONSerialization.jsonObject(with: Data(tJsonObj.utf8)) as? [[String:Any]] { 
      for item in innerJSON { 
       print(item) 
      } 
     } 
    } 
} catch { 
    print(error) 
} 
+0

Merci beaucoup, cette solution fonctionne! – Neneil

0

Le JSON interne pour d semble échappé. JSON valide doit ressembler à quelque chose comme:

{"d": "[{"title":"xxx","timestamp":"2017-10-16 23:53:40"},... 

D'où vient votre JSON?

+0

Le JSON venant de mon serveur. Les objets sont créés avec JavaScriptSerializer. Je suis sûr que le JSON est valide. – Neneil